多重继承下的“四不像”

“四不像”是 麋鹿 的绰号。由于麋鹿长相非常特殊,它的犄角像 鹿 ,面部像 ,蹄子像 ,尾巴像 ,但整体看上去却似鹿非鹿,似马非马,似牛非牛,似驴非驴,故获得“四不像”的美名。
02000737_ZxRJ.jpeg 以上为对四不像的特征描述,那么我们要用java类如何描述呢,看以下代码:

定义

正常的鹿、马、牛、驴,并用int表示特征相似度。
public interface Deer{ int horn(); } public interface Horse{ int face(); } public interface Bull{ int hoof(); } public interface Donkey{ int tail(); }

实现

我们对正常的鹿、马、牛、驴实现如下定义(此处不去详细考虑种类)
public class DeerImpl implements Deer{ public int horn(){ return 5; } } public class HorseImpl implements Horse{ public int face(){ return 5; } } public class BullImpl implements Bull{ public int hoof(){ return 5; } } public class DonkeyImpl implements Donkey{ public int tail(){ return 5; } }
正如上述代码所示,我们定义了正常的鹿的角与标准的相似度为5,正常马的面部与标准相似度为5,正常牛的蹄子与标准相似度为5,正常驴的尾巴与标准相似度为5.

实现四不像

前面我们已经介绍过四不像学名叫“麋鹿”,那么接下来我们将用麋鹿的英文名进行定义:
public class PereDavidDeer extends DeerImpl implements Horse,Bull,Donkey { @Override public int horn(){ return super.horn()-1; } @Override public int face(){ return new HorseSpecial().face(); } @Override public int hoof(){ return new BullSpecial().hoof(); } @Override public int tail(){ return new DonkeySpecial().tail(); } private class HorseSpecial extends HorseImpl{ @Override public int face(){ return super.face()-1; } } private class BullSpecial extends BullImpl{ @Override public int hoof(){ return super.hoof()-1; } } private class DonkeySpecial extends DonkeyImpl{ @Override public int tail(){ return super.tail()-1; } } }
如上,利用java的内部类特性即实现来“四不像”的定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不爱运动的跑者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值