How to find out the subclass from the base class i

2020-03-01 04:57发布

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?

6条回答
闹够了就滚
2楼-- · 2020-03-01 05:38

Have you tried using 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.
查看更多
Animai°情兽
3楼-- · 2020-03-01 05:41

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 simple approach.

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:

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.

查看更多
何必那么认真
4楼-- · 2020-03-01 05:43

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.

Note: Call super(); before setting it

查看更多
太酷不给撩
5楼-- · 2020-03-01 05:46

There are 2 ways I can think of 1) One with Using the Java reflection API 2) Other one would be with the instanceOf

Other method can be a Comparing objects to objects, I dont know how it might be, you can try this

查看更多
6楼-- · 2020-03-01 05:49

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.

查看更多
萌系小妹纸
7楼-- · 2020-03-01 06:03

using either instanceof or 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

Inside the if-clause you'd need to downcast - i.e.

((B) returned).doSomethingSpecificToB();

That said, sometimes it is considered that using instanceof or getClass() is a bad practice. You should use polymorphism to try to avoid the need to check for the concrete subclass, but I can't tell you more with the information given.

查看更多
登录 后发表回答