Java 如何从基类实例中找出子类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2856122/
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
How to find out the subclass from the base class instance?
提问by Vaishak Suresh
Is there a way to find out the name of derived class from a base class instance?
有没有办法从基类实例中找出派生类的名称?
e.g.:
例如:
class A{
....
}
class B extends A{
...
}
class c extends A{
...
}
now if a method returns an object of A
, can I find out if it is of type B
or C
?
现在如果一个方法返回一个对象A
,我可以找出它是类型B
还是C
?
采纳答案by Bozho
using either instanceof
or Class#getClass()
使用instanceof
或Class#getClass()
A returned = getA();
if (returned instanceof B) { .. }
else if (returned instanceof C) { .. }
getClass()
would return either of: A.class
, B.class
, C.class
getClass()
将返回以下任一项:A.class
, B.class
,C.class
Inside the if-clause you'd need to downcast - i.e.
在 if 子句中,您需要向下转型 - 即
((B) returned).doSomethingSpecificToB();
That said, sometimes it is considered that using instanceof
or getClass()
is a bad practice. You should use polymorphismto try to avoid the need to check for the concrete subclass, but I can't tell you more with the information given.
也就是说,有时认为使用instanceof
或getClass()
是一种不好的做法。您应该使用多态性来尽量避免检查具体子类的需要,但我无法通过所提供的信息告诉您更多信息。
回答by Buhake Sindi
Have you tried using instanceof
你有没有试过使用 instanceof
e.g.
例如
Class A aDerived= something.getSomethingDerivedFromClassA();
if (aDerived instanceof B) {
} else if (aDerived instanceof C) {
}
//Use type-casting where necessary in the if-then statement.
回答by Timothy
Short answer to your question
简短回答你的问题
Is there a way to find out the derived class's name from a base class object?
有没有办法从基类对象中找出派生类的名称?
no, the super-class has no way of telling the name/type of a sub-class.
不,超类无法告诉子类的名称/类型。
You have to interrogate the object (which is an instance of a sub-class) and ask if it is an: instanceof
a particular sub-class, or call it's getClass()
method.
你必须询问对象(它是一个子类的实例)并询问它是否是一个:instanceof
一个特定的子类,或者调用它的getClass()
方法。
回答by gmhk
There are 2 ways I can think of 1) One with Using the Java reflection API 2) Other one would be with the instanceOf
我可以想到两种方法 1)一种使用 Java 反射 API 2)另一种使用 instanceOf
Other method can be a Comparing objects to objects, I dont know how it might be, you can try this
其他方法可以是一个比较对象与对象,我不知道它可能是怎么回事,你可以试试这个
回答by Mario Gravel
You can do it in the subclass' constructor
您可以在子类的构造函数中执行此操作
class A {
protected String classname;
public A() { this.classname = "A"; }
public String getClassname() { return this.classname; }
}
class B extends A {
public B() {
super();
this.classname = "B";
}
}
So
所以
A a = new A();
a.getClassname(); // returns "A"
B b = new B();
b.getClassname(); // returns "B"
((A)b).getClassname(); // Also returns "B"
Because it is casted into an "A" object, it will call the "A" getClassname()
function but will return a value set by the constructor that was the "B" constructor.
因为它被转换成一个“A”对象,它将调用“A”getClassname()
函数,但会返回一个由作为“B”构造函数的构造函数设置的值。
Note: Call super();
before setting it
注意:super();
设置前调用
回答by Marko Pacak
Is there a way to find out the name of derived class from a base class instance?
有没有办法从基类实例中找出派生类的名称?
As answered here, you can use this extremely simpleapproach.
正如这里所回答的,您可以使用这种极其简单的方法。
abstract class A {
public final String getName() {
return this.getClass().getName();
}
}
class B extends A { }
class C extends A { }
then simply print the current class
name:
然后简单地打印当前class
名称:
B b = new B();
C c = new C();
System.out.println(b.getName());
System.out.println(c.getName());
Output:
输出:
com.test.B
com.test.C
There is no need to store additional Strings
, check instanceof
or override
the method in any subclass.
不需要在任何子类中存储额外的Strings
、检查instanceof
或override
方法。