“四不像”是
麋鹿 的绰号。由于麋鹿长相非常特殊,它的犄角像
鹿 ,面部像
马 ,蹄子像
牛 ,尾巴像
驴 ,但整体看上去却似鹿非鹿,似马非马,似牛非牛,似驴非驴,故获得“四不像”的美名。

以上为对四不像的特征描述,那么我们要用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的内部类特性即实现来“四不像”的定义。