- class Person
- {
- private String name;
- private int age;
- Person(){
-
- }
- Person(String name){
-
- this();
- //this.name=name;
- }
- Person(String name,int age){
-
- //this.name=name;
- this(name);
- this.age=age;
- System.out.println("name="+name+"age="+age);
- }
- }
- class PersonDemo4
- {
- public static void main(String[] args)
- {
- Person p=new Person("lisi",30);
- //Person p1=new Person("lisi2",36);
-
- }
- }
复制代码
参数("lisi",30)从主函数传递到构造函数
- Person(String name){
-
- this();
- //this.name=name;
- }
复制代码 之中,在这个函数又用this()调用了最上边的那个构造函数
为什么输出结果依然是name=lisi,age=30?不应该是name=null,age=30么? |
|