How to determine an object's class?

后端 未结 11 1144
醉酒成梦
醉酒成梦 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条回答
  •  -上瘾入骨i
    2020-11-22 17:33

    You can use getSimpleName().

    Let's say we have a object: Dog d = new Dog(),

    The we can use below statement to get the class name: Dog. E.g.:

    d.getClass().getSimpleName(); // return String 'Dog'.

    PS: d.getClass() will give you the full name of your object.

提交回复
热议问题