Java编程案例习题= = 求解!

发布网友 发布时间: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());

}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com