在Java中调用子类的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2701182/
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
Call a method of subclass in Java
提问by eyecreate
If I have a base class
Base thing = null;
of which there is a subclass
class Subclass extends Base
and I instigate it as
thing = new Subclass
how would I call a method that is specifically in Subclass, but not in Base?
ex.
Base
has only method()
Subclass
has method()
and specialMethod()
the method specialMethod()
is the one I want to call.
如果我有一个基类
Base thing = null;
,其中有一个子类,
class Subclass extends Base
并且我煽动它作为
thing = new Subclass
如何调用专门在子类中而不是在 Base 中的方法?前任。
Base
has only method()
Subclass
hasmethod()
并且specialMethod()
方法specialMethod()
是我要调用的方法。
回答by Eric Eijkelenboom
If you know that thing
contains a Subclass
, you can do:
如果您知道thing
包含 a Subclass
,则可以执行以下操作:
((Subclass) thing).specialMethod()
回答by Yishai
You have to type, or cast thing to the subclass. So:
您必须键入或将内容转换为子类。所以:
Subclass thing = new Subclass();
or:
或者:
((Subclass) thing).specialMethod();
回答by OscarRyz
You have to cast
it to be able to invoke the method:
您必须使用cast
它才能调用该方法:
Base thing = new SubClass();
((SubClass) thing ).specialMethod();
If you face this situation, most likely you don't have the right interface ( the right set of methods )
如果您遇到这种情况,很可能您没有正确的接口(正确的方法集)
Before going deep into the stage where you start validating everything to know if you can invoke a method or not like in:
在深入到开始验证所有内容以了解是否可以调用方法的阶段之前:
public void x ( Base thing ) {
if( thing.instanceof Subclass ) {
((SubClass)thing).specialMethod();
}
}
Consider if you don't need to move the specialMethod
up in the hierarchy so it belongs to the base.
考虑您是否不需要specialMethod
在层次结构中向上移动使其属于基础。
If you definitely don't need it in the base, but you need it in the subclass at least consider use the correct type:
如果您在基类中绝对不需要它,但在子类中需要它,至少考虑使用正确的类型:
SubClass thing = ...
// no need to cast
thing.specialMethod();
But as always, this depends on what are you trying to do.
但与往常一样,这取决于您要尝试做什么。
回答by artgon
When dealing with inheritance/polymorphism in Java there are basically two types of casts that you see:
在 Java 中处理继承/多态性时,您会看到基本上有两种类型的强制转换:
Upcasting:
上行:
Superclass x = new Subclass();
This is implicit and does not need a hard cast because Java knows that everything the Superclass
can do, the Subclass
can do as well.
这是隐含的,不需要强制转换,因为 Java 知道Superclass
可以做的所有事情,Subclass
也可以做。
Downcasting
垂头丧气
Superclass x = new Subclass();
Subclass y = (Subclass) x;
In this case you need to do a hard cast because Java isn't quite sure if this will work or not. You have to comfort it by telling it that you know what you're doing. The reason for this is because the subclass could have some weird methods that the superclass doesn't have.
在这种情况下,您需要进行强制转换,因为 Java 不太确定这是否可行。你必须通过告诉它你知道你在做什么来安慰它。这样做的原因是因为子类可能有一些超类没有的奇怪方法。
In general, if you want to instantiate a class to call something in its subclass, you should probably just instantiate the subclass to begin with -- or determine if the method should be in the superclass as well.
一般来说,如果你想实例化一个类来调用它的子类中的某些东西,你可能应该只是实例化子类,或者确定该方法是否也应该在超类中。
回答by Esko Luontola
The others have already mentioned how to cast objects to get an answer to your question, but asking that question in the first placepoints to a possible design problem. Some possible reasons:
其他人已经提到了如何投射对象以获得问题的答案,但首先提出这个问题指向了一个可能的设计问题。一些可能的原因:
- The method is in the wrong place.
- The code which calls the method is in the wrong place.
- The subclass should not extend the other class. It's best to prefer composition over inheritance. And when inheriting, the code should follow the Liskov substitution principle.
- The classes are non-cohesive, they have more than one responsibility, and they should be split into multiple classes.
回答by Tom Stein
Another approach might be to do the following:
另一种方法可能是执行以下操作:
public abstract class Base {
//method() not implemented
public abstract void specialMethod();
}
public class Subclass extends Base {
//method() not implemented
@Override
public void specialMethod() {
//put your code here
System.out.println("specialMethod from Subclass");
}
}
So you can do:
所以你可以这样做:
thing.specialMethod();
and it will give you: "specialMethod from Subclass".
它会给你:“来自子类的特殊方法”。
回答by ferics
You could make the method you would like to call being an abstract method and have it implemented in the subclass. Then in your superclass, just call it as usual this.someMethod()
. At runtime, it will go to someMethod()
and run the code that is implemented from the subclass.
您可以将要调用的方法设为抽象方法,并在子类中实现它。然后在你的超类中,像往常一样调用它this.someMethod()
。在运行时,它将转到someMethod()
并运行从子类实现的代码。