public class Dog {
private int age = 0;
private String name = "no-name";
private String skill = "no-skill";
public Dog(String name) {
this.name = name;
}
public Dog(int age) {
this.age = age;
}
public Dog(String name, int age) {
// ! Dog(name); //只用能this()的方式调用
this(name);
// ! this(age); //构造函数只能调用一次
this.age = age;
}
@Override
public String toString() {
return " A dog,namne:" + name + ",age:" + age + ",skill:" + skill;
}
public static void main(String[] args) {
Dog Kimme = new Dog("Kimme", 1);
Dog Robot = new Dog("Robot");
Dog Feed = new Dog(5);
System.out.println(Kimme);
System.out.println(Robot);
System.out.println(Feed);
}
}
5.4.1 构造器中调用构造器
最新推荐文章于 2024-02-22 14:53:57 发布