java 使用基类对象调用派生类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15299686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling derived class method using base class object
提问by Raj
I have 6 classes as shown in figure below.
Now, class A
has an object of class B
as a private variable defined. Also class A
methods calls many methods from class B, for example B.method1()
.
Now, class A_Base1
is which is derived from class A
, needs to call methods from the derived class B_Base1
; for example B1.method2()
. And also methods of class A_Base2
needs to call methods from class B_Base2
; for example B2.method3()
.
我有 6 个类,如下图所示。现在,定义了一个class A
类对象B
作为私有变量。类A
方法也调用 B类中的许多方法,例如B.method1()
. 现在, classA_Base1
是从 class 派生的A
,需要调用派生类的方法B_Base1
;例如B1.method2()
。而且类的方法也A_Base2
需要从类中调用方法B_Base2
;例如B2.method3()
。
Now in class A I define the variable as -
现在在 AI 类中将变量定义为 -
private B bObject
private B bObject
Now in method of A_Base1
, I cannot cannot call the methods like bObject.method2()
since its a base class object.
现在在 的方法中A_Base1
,我不能像bObject.method2()
它一样调用方法,因为它是一个基类对象。
I need suggestions on -
我需要关于-的建议
Is it possible to call derived class object methods using base class object? Or do I need to re-design this scenario in some other good way?
是否可以使用基类对象调用派生类对象方法?或者我是否需要以其他一些好的方式重新设计这个场景?
采纳答案by wallenborn
Using inheritance like this imo only makes sense if the Bx.methodX() do something that means she same to the different Ax. And in that case, you should name them that way:
仅当 Bx.methodX() 做了一些意味着她对不同的 Ax 相同的事情时,像这样使用 imo 继承才有意义。在这种情况下,您应该这样命名它们:
public class B {
public void doWhatAMeans() {
method1();
}
public class B1 extends B {
@Override
public void doWhatAMeans() {
method2();
}
public class B2 extends B {
@Override
public void doWhatAMeans() {
method3();
}
and then you only need A to call doWhatAMeans() and the A1 and A2 only need to be injected the appopriate instances of Bx.
然后你只需要 A 调用 doWhatAMeans() 并且 A1 和 A2 只需要注入 Bx 的适当实例。
On the other hand, if doWhatAMeans does not make sense because the methodX do different things that mean different things to Ax, then you need to rethink your object model, probably the parallel structures A,A1,A2 and B,B1,B2 are wrong then.
另一方面,如果 doWhatAMeans 没有意义,因为 methodX 做了不同的事情,对 Ax 意味着不同的事情,那么你需要重新考虑你的对象模型,可能并行结构 A,A1,A2 和 B,B1,B2 是错误的然后。
回答by radai
you could always cast. suppose your class A provides this method:
你总是可以投射。假设你的 A 类提供了这个方法:
protected B getBInstance() {
return bObject;
}
then in A_Base1 you could do something like:
然后在 A_Base1 中,您可以执行以下操作:
((B_Base1)getBInstance()).method2();
this, however, is a VERY bad design. if your A_Base1 class needs an instance of B_Base1 it should be handed such an instance directly at construction time:
然而,这是一个非常糟糕的设计。如果您的 A_Base1 类需要 B_Base1 的实例,则应在构建时直接将此类实例交给它:
public class A_Base1 extends A {
private B_Base1 b1Object;
public A_Base1(B_Base1 instance) {
super(B_Base1); //works as a B for parent
this.b1Ovject = instance;
}
}
and then you can use that
然后你可以使用它
回答by Matt Westlake
since A is a parent of A_Base1 (I'm assuming extended) you can make the function call that Accesses B public (or protected) and then A_Base1 or A_Base2 can use the same function A does to call into B.
由于 A 是 A_Base1 的父级(我假设是扩展的),因此您可以进行访问 B 公开(或受保护)的函数调用,然后 A_Base1 或 A_Base2 可以使用 A 调用 B 的相同函数。