public class MyDog5 extends MyPetTest3 {
public MyDog5(String name, int age, String color, double weight) {
super(name, age, color, weight);//调用父类的构造方法
}
private String species;//子类扩展的属性:品种
public MyDog5(String name, int age, String color, double weight,String species) {//构造方法重载
super(name, age, color, weight);//调用父类的构造方法
this.species=species;
}
//给属性species添加get和set方法
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//宠物狗独有的方法,美容
public void cosmetic(){
System.out.println(this.getName()+"每周都要到宠物店做一次美容。");
}
public void show(){//重写父类的show方法
System.out.println(this.getName()+"今年"+this.getAge()+"岁,是"+this.getColor()+"颜色的"+
this.getSpecies()+",体重是"+this.getWeight());
}
public void speak(){//重写父类的speak方法
System.out.println(this.getName()+":汪汪汪");
}
public static void main(String[] args) {
MyDog5 dog=new MyDog5("阿发",3,"黑色",3.8,"贵宾犬");
dog.show();
dog.speak();
dog.cosmetic();
dog.playWithOwnner("星星");
}
}
面向对象-宠物狗实例
最新推荐文章于 2024-11-19 10:49:52 发布