构造函数实例化

package com.java1234.chap03.sec04;

public class People {
    
    //string类的默认值是null
    private String name;
    //int类属性默认值是0;
    private int age;
    
    /*
     * 默认构造函数*/
     People() {
        // TODO Auto-generated constructor stub
        System.out.println("默认构造方法!");
    }
    
    /*
     * 有参数的构造方法
     */
    People(String name2,int age2){
        name=name2;
        age=age2;
        System.out.println("有参数的构造方法!");
    }
    
    public void say(){
        System.out.println("我叫:"+name+",我今年:"+age);
    }    
    
    
    public static void main(String[] args) {
        People people=new People();
        People people2=new People("张三",23);
        people.say();
        people2.say();
    }

}


执行结果:

默认构造方法!
有参数的构造方法!
我叫:null,我今年:0
我叫:张三,我今年:23

 

 

posted on 2015-03-15 12:30  尼欧  阅读(180)  评论(0编辑  收藏  举报