汇才同飞教育汇才同飞学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › 17600202212 › 日志

设计一个练习,说明instanceof的用法  

2019-02-13 20:21:34 查看(299) 回复(0)

 

package myinstanceof;


public class dianqi {
String name;


}
package myinstanceof;


public class dianshi extends dianqi {
public void see() {
System.out.println("看电视");
}


}

package myinstanceof;


public class xiyiji extends dianqi {
public void work() {
System.out.println("洗衣服");
}


}

package myinstanceof;


public class bingxiang extends dianqi{
public void eat() {
System.out.println("香蕉苹果");
}


}

package myinstanceof;


public class banjia {


public void ban(dianqi d) {
if (d instanceof dianshi) {
System.out.println(d.name+"搬到客厅");
dianshi d1=(dianshi)d;
d1.see();
} else if (d instanceof xiyiji) {
System.out.println(d.name+"搬到卫生间");
} else if (d instanceof bingxiang) {
System.out.println(d.name+"搬到厨房");
}


}
}

package myinstanceof;


public class Tset {


public static void main(String[] args) {
banjia b1 = new banjia();
dianqi d1 = new dianshi();
dianqi d2 = new bingxiang();
dianqi d3 = new xiyiji();
d1.name="电视机";
b1.ban(d1);
d2.name="冰箱";
b1.ban(d2);
d3.name="洗衣机";
b1.ban(d3);


}


}

评论 (0 个评论)

facelist

全部作者的其他最新日志

站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

返回顶部