java第十一天作业
程序员文章站
2022-05-23 08:37:28
...
作业如下图:
程序如下:
代码:
public class Homework6{
public static void main(String[] args){
Master m = new Master();
Pet d = new Dog();
Pet c = new Cat();
Pet y = new YingWu();
m.feed(d);
m.feed(c);
m.feed(y);
}
}
class Master{
public void feed(Pet p){
p.eat();
}
}
class Pet{
public void eat(){
System.out.println("宠物在吃东西");
}
}
class Dog extends Pet{
public void eat(){
System.out.println("狗在吃狗粮");
}
}
class Cat extends Pet{
public void eat(){
System.out.println("猫在吃猫粮");
}
}
class YingWu extends Pet{
public void eat(){
System.out.println("鹦鹉在吃鸟粮");
}
}