发布网友 发布时间:2022-04-23 09:59
共1个回答
热心网友 时间:2023-10-10 13:57
//父类:动物类
class Animal{
String name;
Integer age;
String food;
Animal(String name,Integer age,String food){
this.name=name;
this.age=age;
this.food=food;
}
//省略getter.setter
}
//飞的接口
public interface Fly{
public void fly();
}
//黄狗(继承,接口)
class Dog extends Animal implements Fly{
public Dog(){}
public Dog(String name,Integer age,String food){
super(name,age,food);
}
@Overide
public void fly(){
Sysote.out.println("我不会飞");
}
}
//白猫
class Cat extends Animal implements Fly{
public Cat(){}
public Cat(String name,Integer age,String food){
super(name,age,food);
}
@Overide//重写方法
public void fly(){
Sysote.out.println("我会飞");
}
}
//测试
public class Test{
pulbic static void main(String[] args){
//多态实例化对象
//构造方法赋值
Animal dog = new Dog("黄金狗",15,"肉");
Animal cat = new Cat("白猫",11,"鱼");
System.out.println("我是"+cat.getName+",我"+cat.getAge()+"岁,我吃"+cat.getFood()+","+cat.fly());
System.out.println("我是"+dog.getName+",我"+dog.getAge()+"岁,我吃"+cat.getFood()+","+dog.fly());
}
}