class Animal{
int id;
String name;
String color;
public Animal(int id,String name,String color){
this(name,color); //调用到两个参数的构造函数
this.id = id;
}
public Animal(String name,String color){
this.name = name;
this.color =color;
}
}
public class Demo42 {
public static void main(String[] args) {
Animal a1 = new Animal("哈巴狗","白色");
Animal a2 = new Animal(999,"沙皮狗","米黄色");
System.out.println("名字:"+a1.name+" 颜色:"+ a1.color);
System.out.println("编号:"+ a2.id+ " 名字:"+a2.name+" 颜色:"+ a2.color);
}
}
JAVA小练习42——this关键字练习(2)
最新推荐文章于 2023-12-26 17:10:47 发布