Is there a way to determine what type a class is an instance of in Java?

后端 未结 5 2066
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 04:11

Say I have 3 classes like so:

class A {}
class B extends A {}
class C extends A {}

Would it then be possible to determine whether a particu

相关标签:
5条回答
  • 2020-12-12 04:20

    You also might want to look at the double dispatch idiom which is an OO way of changing behaviour based on the type of an argument in languages which don't support multi-methods.

    0 讨论(0)
  • 2020-12-12 04:22

    Do this:

    if (myObject instanceof B) {
        // do something for B
    } else (myObject instanceof C) {
        // do something for C
    } else {
        // do something for A
    }
    
    0 讨论(0)
  • 2020-12-12 04:24

    you could do this

    if (myObject.getClass() == B.class) {
        // do something for B (not subclasses of b!!!)
    } else if(myObject.getClass() == C.class) {
        // do something for C (not subclasses of c!!!)
    } else if(myobject.getClass() == A.class) {
        // do something for A (not subclasses of A!!)
    } else if(myobjects instanceof A){
       //all other subclasses of A
    }
    
    0 讨论(0)
  • 2020-12-12 04:42

    The simpler and faster code is:

    if (myObject instanceof B) {
    } else if (myObject instanceof C) {
    } else if (myObject instanceof A) {
    }
    

    Note that the order is important: you have to have the test for A last, as that will succeed for instances of B and C as well.

    However, your original code would nearly work. Class.isInstance checks whether the value really is an instance of the given class or any superclass. So if myObject is an instance of C, then B.class.isInstance(myObject) will return false. All you've got wrong is that you're calling getClass() on myObject unnecessarily, instead of using B.class etc.

    This is the approach you would take if you didn't know which classes you were interested in at compile time - the instanceof operator only works when you can specify the type statically in code.

    Now if you want to find out whether myObject is an instance of exactly B (and not a subclass) then just use:

    if (myObject.getClass() == B.class)
    

    (This will blow up if myObject is a null reference, of course.)

    0 讨论(0)
  • 2020-12-12 04:43

    There is the instanceof operator that does what you want, as others have answered.

    But beware that if you need something like this, it's a sign that there might be a flaw in the design of your program. The better, more object oriented way to do this is by using polymorphism: put a method in the superclass A, override that method in B and C, and call the method on myObject (which should have type A):

    A myObject = ...;  // wherever you get this from
    
    // Override someMethod in class B and C to do the things that are
    // specific to those classes
    myObject.someMethod();
    

    instanceof is ugly and should be avoided as much as possible, just like casting is ugly, potentially unsafe and should be avoided.

    0 讨论(0)
提交回复
热议问题