Day11多态部分-2-1

package Day11;


public class Test_022 {


public static void main(String[] args) {
Animal1 c = new Cat1(); 
show(c); 
/*Day11.Dog1 cannot be cast to Day11.Cat1 Day11。
狗1不能被扔到Day11.Cat1 
 Dog1  dd  = (Dog1) d;   
show(d);*/
Animal1 d =  new Dog1();
show(d);
Animal1 p = new Pig1();
show(p);
}
public static void show(Animal1 tmp){//1.进来的是Animal1的引用
tmp.eat(); 

// Cat1 t = (Cat1) tmp;//(向下转型 )2.转成Cat1的引用
// t.show1();//3.再调Cat1特有的功能

if(tmp instanceof Cat1){//如果tmp进来的是猫就
Cat1 t = (Cat1) tmp;
t.show1();
}else if(tmp instanceof Dog1){//否则 如果进来的是狗就
Dog1 d =(Dog1) tmp;
d.show2();
}else if(tmp instanceof Pig1){
Pig1 p =(Pig1) tmp;
p.show3();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值