访问接口中的子类

huangapple go评论62阅读模式
英文:

Access a subclass from an interface

问题

我有一个名为Component的接口的数组列表。另有一个名为Frame的类,它如下所示继承自component:Component(接口)>AbstractComponent(抽象类)>AbstractContainer(抽象类)>OrganizedContainer(类)>Frame(类)

组件数组列表中的所有内容都会传递给渲染器进行渲染。我希望能够从AbstractContainer类中访问Frame对象的方法。在不在Component接口中声明该方法的情况下,是否有可能实现这一点?

英文:

I have an arraylist of an interface named Component. There is another class named Frame that extends component as follows: Component (interface) > AbstractComponent (abstract class) > AbstractContainer (abstract class) > OrganizedContainer (class) > Frame (class)

Everything in the component arraylist goes to a renderer to be rendered. I want to be able to access the methods of the Frame object from the AbstractContainer class. Is this possible without declaring the method in the Component interface?

答案1

得分: 4

一个类 实现 一个接口。一个类 继承 另一个类。一个接口 继承 另一个接口。这是Java中的约定,正如在您的问题中提到的,您说您的类扩展了接口 Component

Frame<<具体类>>扩展了OrganizedContainer,OrganizedContainer扩展了AbstractContainer,AbstractContainer实现了AbstractComponent并实现了Component<<接口>>

现在来看看您的问题。在 AbstractContainer 类内部是否可以访问仅在 Frame 类中定义的方法?是的,是可能的。
怎么做?在父类中,只需进行类型转换 (Frame)this.methodInFrame(),为了避免其他某个子类对象成为 this 当前引用的对象,您需要使用 instanceof 操作符。所以像这样:

 if(this instanceof Frame){
     (Frame)this.methodInFrame();
 }

这是一个好的做法吗?不,一点也不好,超类不应依赖于其子类。这会增加程序中的耦合并导致维护问题。如果您现在或将来可能拥有任何其他具体的子类,会怎么样?当您修改 Frame 时,您的超类也需要进行修改,由此您新增的子类也许需要修改。此外,这会导致可能是运行时问题的问题。无论何时使用 instanceof,请注意这是一种代码异味,您必须有非常充分的理由才能使用它。

英文:

A Class implements an interface. A Class extends another Class. An Interface extends another interface. This is the convention in java and I mentioned as in your question you have said your class extends the interface Component.

Frame&lt;&lt;concrete class&gt;&gt; extends OrganizedContainer extends AbstractContainer extends AbstractComponent implements Component&lt;&lt;interface&gt;&gt;

Now coming to your question. Is it possible from inside the AbstractContainer Class to access the method which are only defined in Frame ? Yes it is possible.
How ? In the parent just typecast the (Frame)this.methodInFrame() and to avoid some other sub class object being the currently referred by this, you need to use the instanceof operator. so its like

 if(this instanceof Frame){
     (Frame)this.methodInFrame();
 }

Is it a good practice ? No not at all, Super class should not be dependent on its sub classes. This increases coupling in your program and leads to maintenance issues. What if you have or plan to have or may have in future any more concrete sub classes ? When you modify the Frame your super classes needs to be modified and due to that your newly added subclasses may need modification. Also this leads to issues which may be runtime issues. when ever you use instanceof note that it is a code smell, you must have very good reason to use it.

答案2

得分: 1

唯一的方法是检查组件是否为 Frame 类型,使用 component instanceof Frame,然后将组件转换为 Frame

英文:

The only way to do that is check if the component is of type Frame with component instanceof Frame and then cast the component to Frame.

huangapple
  • 本文由 发表于 2020年5月29日 14:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/62080141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定