关于接口的一些思考
2015-04-17 09:13
295 查看
下面示例是模拟游戏《愤怒的小鸟》的实现。
//叫的方式的接口
public interface ShoutType {
public void shout();
}
//嗷嗷叫
public class AoShout implements ShoutType {
public void shout() {
System.out.println("嗷嗷叫");
}
}
//喳喳叫
public class ZhaShout implements ShoutType {
@Override
public void shout() {
System.out.println("喳喳叫");
}
}
//鸟的抽象类
public abstract class Bird {
ShoutType shout_type;//接口作为属性
public Bird(ShoutType shout_type) {
super();
this.shout_type = shout_type;
}
public void shout() {
shout_type.shout();// 调用接口的方法
}
public void fly() {
System.out.println("弹射飞");
}
public abstract void attact();
}
//炸弹鸟
public class BombBird extends Bird {
public BombBird(ShoutType shout_type) {
super(shout_type);
}
public void attact() {
System.out.println("炸弹攻击");
}
}
//分裂鸟
public class SplitBird extends Bird {
public SplitBird(ShoutType shout_type) {
super(shout_type);
}
public void attact() {
System.out.println("分裂攻击");
}
}
//测试类
public class BirdTest {
public static void main(String[] args) {
ShoutType ao_shout = new AoShout();
ShoutType zha_shout = new ZhaShout();
Bird bomb = new BombBird(zha_shout);
Bird split = new SplitBird(ao_shout);
bomb.shout();
split.shout();
}
}
这个示例是将接口的对象作为抽象类的一个属性,并且在构造方法中对其初始化,在各个子类中调用父类的构造方法,实现对叫的方式的初始化。
一种用法是定义一个方法,将接口作为参数传进来,方法体里面直接调用接口里的抽象方法。
另外一种用法是将实现接口的类以接口作为类型进行强制转换。(Interface)sub_class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
定义接口:
interface Fight{
void fight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
肥肥和瘦瘦去实现这个接口:
class FatFat implements Fight{
public void fight(){
System.out.println("FatFat 打人很痛!");
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class ThinThin implements Fight{
public void fight(){
System.out.println("ThinThin 打人一点都不痛!!哈哈。");
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后你可能会这另一个类中使用到FatFat和ThinThin的对象,然后都去执行fight,但是你可能不到运行时就不会知道是具体的那个类的对象,这是你就感谢他们都实现了Fight接口,你可以向上转型,然后通过运行时的多态产生你想要的行为。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Fight a=new FatFat();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Fight b=new ThinThin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.fight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b.fight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这样就会执行不同的动作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
或者如果你有一个方法
f(Fight i){
i.fight();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
如果c是实现了Fight接口的其中一个类,那么你就可以这样使用这个方法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f(c);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
你不需要知道c究竟是什么对象(不管是FatFat还是ThinThin),你都可以得到你想要的fight动作。
//叫的方式的接口
public interface ShoutType {
public void shout();
}
//嗷嗷叫
public class AoShout implements ShoutType {
public void shout() {
System.out.println("嗷嗷叫");
}
}
//喳喳叫
public class ZhaShout implements ShoutType {
@Override
public void shout() {
System.out.println("喳喳叫");
}
}
//鸟的抽象类
public abstract class Bird {
ShoutType shout_type;//接口作为属性
public Bird(ShoutType shout_type) {
super();
this.shout_type = shout_type;
}
public void shout() {
shout_type.shout();// 调用接口的方法
}
public void fly() {
System.out.println("弹射飞");
}
public abstract void attact();
}
//炸弹鸟
public class BombBird extends Bird {
public BombBird(ShoutType shout_type) {
super(shout_type);
}
public void attact() {
System.out.println("炸弹攻击");
}
}
//分裂鸟
public class SplitBird extends Bird {
public SplitBird(ShoutType shout_type) {
super(shout_type);
}
public void attact() {
System.out.println("分裂攻击");
}
}
//测试类
public class BirdTest {
public static void main(String[] args) {
ShoutType ao_shout = new AoShout();
ShoutType zha_shout = new ZhaShout();
Bird bomb = new BombBird(zha_shout);
Bird split = new SplitBird(ao_shout);
bomb.shout();
split.shout();
}
}
这个示例是将接口的对象作为抽象类的一个属性,并且在构造方法中对其初始化,在各个子类中调用父类的构造方法,实现对叫的方式的初始化。
一种用法是定义一个方法,将接口作为参数传进来,方法体里面直接调用接口里的抽象方法。
另外一种用法是将实现接口的类以接口作为类型进行强制转换。(Interface)sub_class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
定义接口:
interface Fight{
void fight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
肥肥和瘦瘦去实现这个接口:
class FatFat implements Fight{
public void fight(){
System.out.println("FatFat 打人很痛!");
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class ThinThin implements Fight{
public void fight(){
System.out.println("ThinThin 打人一点都不痛!!哈哈。");
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后你可能会这另一个类中使用到FatFat和ThinThin的对象,然后都去执行fight,但是你可能不到运行时就不会知道是具体的那个类的对象,这是你就感谢他们都实现了Fight接口,你可以向上转型,然后通过运行时的多态产生你想要的行为。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Fight a=new FatFat();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Fight b=new ThinThin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.fight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b.fight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这样就会执行不同的动作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
或者如果你有一个方法
f(Fight i){
i.fight();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
如果c是实现了Fight接口的其中一个类,那么你就可以这样使用这个方法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f(c);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
你不需要知道c究竟是什么对象(不管是FatFat还是ThinThin),你都可以得到你想要的fight动作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 关于接口设计的一些思考
- 关于“JAVA中为什么没有了多继承并出现了接口”这一问题引发的一些思考
- 关于软件开发和模块接口设计之一些思考
- 前后端交互-一些关于接口设计的思考
- 关于面向构件和EOS的一些思考-xml和元数据解决了接口脆弱性问题吗?
- 关于面向对象的一些思考
- (转)看了一些ASP.NET MVC开源项目后的一些想法,关于ASP.NET MVC+Repository+Service架构的一些思考
- 关于重载、隐藏、重写的一些思考(1)
- 关于SpringIOC的一些思考
- 关于sizeof()的一些思考
- 《Pro Git》笔记二:关于远程仓库的一些思考和记录
- 关于网站App缩略图的一些思考
- 十问 TiDB :关于架构设计的一些思考
- 关于AdaBoost的一些再思考
- 关于寻路算法的一些思考(8):长期和短期目标
- 关于企业内部系统访问地址的一些思考
- 关于redux+react的一些思考
- 不只是休闲:关于体感游戏的一些思考(三) --- 射击
- [体感游戏]关于体感游戏的一些思考(五) --- 导航
- 关于开发的一些思考