this构造器体现程序的复用
package 构造器;
public class Person {
String name;
int age;
String id;
//空构造
public Person() {
//this("x",21);//this()这个是调用其他构造器,可以提高代码的复用性
System.out.println("zero 参数");
}
//两个参数构造器
public Person(String name,int age) {
System.out.println("two 参数");
this.name =name;
this.age =age;
}
//三个参数构造器
public Person(String name,int age,String id) {
System.out.println("there 参数");
this.name = name;
this.age =age;
this.id = id;
}
}
package 构造器;
public class test {
public static void main(String[] args) {
Person p =new Person();
}
}
//console
two 参数
zero 参数
例子2.
package 构造器;
public class Person {
String name;
int age;
String id;
//空构造
public Person() {
//this("x",21);//this()这个是调用其他构造器,可以提高代码的复用性
System.out.println("zero 参数");
}
//两个参数构造器
public Person(String name,int age) {
System.out.println("two 参数");
this.name =name;
this.age =age;
}
//三个参数构造器
public Person(String name,int age,String id) {
this("",0);
System.out.println("there 参数");
// this.name = name;
// this.age =age;
this.id = id;
}
}
package 构造器;
public class test {
public static void main(String[] args) {
Person p =new Person("x",21,"612429");
}
}
//console
two 参数
there 参数