多态练习一

public class Animal {
String name;


public Animal() {
    super();
}


public Animal(String name) {
    super();
    this.name = name;
}


public void eat(){
    System.out.println("正在吃");
}
}
public class Cat extends Animal {
    public void eat(){
        System.out.println("我是小猫,我在吃鱼");
    }

    public void zhua(){
        System.out.println("抓老鼠把");
    }
    
    
}
public class Dog extends Animal {
public void eat(){
    System.out.println("我是小狗,我在吃骨头");
}
public void kan(){
    System.out.println("小狗要看家的");
}

}
public class Eagle extends Animal {

public void eat(){
    System.out.println("我是老鹰,我也不知道自己要吃点啥");
}


public void fly(){
    System.out.println("飞翔在天空");
}


}
/*【练习题】1.多态练习
   1.定义一个Animal父类,方法有eat();
    2.定义三个子类;
            Cat,Dog,Eagle;
            每个子类增加新的方法,并重写eat();
    3.利用多态性           wan
            定义一个Animal类型的变量a,并将不同子类的实例赋给a;
            调用eat();观察结果后,并理解多态
    4.思考,如果a的引用指向一个Cat,如何调用Cat的新增方法;*/
public class Test {
public static void main(String[] args) {
    Animal a=new Eagle();
    if(a instanceof Eagle){
        Eagle e=(Eagle)a;
        e.fly();
    }
    a.eat();
    
}
}

 

转载于:https://www.cnblogs.com/zyy1130/p/6705453.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值