[英]Java Interface Design Pattern Situation
I am stuck at a problem. 我陷入一个问题。 My Problem goes like this. 我的问题是这样的。 I have a superclass Animal and two subclasses Human And Bird. 我有一个超类Animal和两个子类Human和Bird。 I have a fly method in my super class Animal which will provide an implementation for both the class Human and Bird based on the Flyable Interface. 我的超类Animal中有一个fly方法,它将基于Flyable Interface为Human和Bird类提供一个实现。
My Animal class looks like this. 我的动物类看起来像这样。
public class`Animal{
public Flyable flyable;
public void fly()
{
flyable.fly();
}
}
Human Class looks like this 人类阶级看起来像这样
class Human extends Animal {
Flyable flyable;
public Human()
{
flyable = new CantFly();
}
}
Bird class looks like this 鸟班看起来像这样
class Bird extends Animal {
Flyable flyable;
public Bird()
{
flyable = new FlyHigh();
}
}
Interfaces are below 接口如下
public interface Flyable {
public void fly();
}
public class CantFly implements Flyable{
@Override
public void fly()
{
Sysout("cant fly");
}
When I call 当我打电话
Animal me = new Human();
me.fly();
It gives me NullPointerException 它给了我NullPointerException
What I m missing here ?. 我在这里想念的是什么?
I was assuming . 我当时以为 。 Since I'm calling new Human() it initializes the Flyable interface in superclass Animal. 由于我正在调用new Human(),因此它将在超类Animal中初始化Flyable接口。 Am I wrong? 我错了吗?
I have solved this problem by changing the design of the method. 我已经通过更改方法的设计解决了这个问题。
fly(Flyable flyable).So I don't want that design solution. fly(Flyable flyable)。因此,我不需要那种设计解决方案。 I remember facing this problem when I was implementing a search algorithm for an app which will provide a list of result but list item will have different 我记得当我为应用程序实现搜索算法时会遇到此问题,该算法将提供结果列表,但列表项将具有不同的结果
UI, 用户界面,
https call to different End URLs, https调用不同的结束URL,
Parsing JSON, 解析JSON,
Mapping to Different POJO classes. 映射到不同的POJO类。
Sadly I had to solve that problem with an alternate approach which I have mentioned. 可悲的是,我不得不用我提到的另一种方法来解决这个问题。
Human
inherits from Animal
, so all its fields are implicitly "copied" from Animal
. Human
从Animal
继承的,因此它的所有字段都是从Animal
隐式“复制”的。 Therefore, you don't need to redeclare flyable
again in Human
and Bird
. 因此,您无需在Human
和Bird
重新声明flyable
。
But you did. 但是你做到了。 This causes the new flyable
fields in the subclasses to hide the original field declared in Animal
. 这将导致子类中的新flyable
字段隐藏 Animal
声明的原始字段。 As a consequence, when you do: 结果,当您这样做时:
flyable = new CantFly();
in Human
, you are assigning a value to the flyable
in Human
, not the flyable
in Animal
. 在Human
,你分配一个值的flyable
的Human
,而不是flyable
的Animal
。
Then you did this: 然后您这样做:
Animal me = new Human();
me.fly();
fly
in Animal
uses the field flyable
that is declared in Animal
class, which has not been assigned yet (you only assigned the flyable
in Human
)! fly
in Animal
使用在Animal
类中声明的flyable
字段,该字段尚未分配(您仅在Human
分配了flyable
)! An NPE occurs as a result. 结果发生了NPE。
To fix this, simply remove all the flyable
fields in the subclasses of Animal
. 要解决此问题,只需删除Animal
子类中的所有flyable
字段。 This way there is only one flyable
field. 这样,只有一个 flyable
领域。
Note 注意
I think this design a little strange. 我认为这种设计有些奇怪。 Human
can't fly, so it shouldn't really have a flyable
field. Human
不会飞,所以它实际上不应有flyable
视野。 In fact, nothing should have a flyable
field. 实际上,任何事物都不应该有flyable
领域。 Among these 3 classes, only Bird
should implement Flyable
. 在这三个类中,只有Bird
应该实现Flyable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.