How to determine an object's class?

后端 未结 11 985
醉酒成梦
醉酒成梦 2020-11-22 17:10

If class B and class C extend class A and I have an object of type B or C, how can I determine of which type

11条回答
  •  伪装坚强ぢ
    2020-11-22 17:48

    You can use:

    Object instance = new SomeClass();
    instance.getClass().getName(); //will return the name (as String) (== "SomeClass")
    instance.getClass(); //will return the SomeClass' Class object
    

    HTH. But I think most of the time it is no good practice to use that for control flow or something similar...

提交回复
热议问题