How to determine an object's class?

2019-01-01 02:59发布

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 it is an instance?

7条回答
浪荡孟婆
2楼-- · 2019-01-01 03:45

Any use of any of the methods suggested is considered a code smell which is based in a bad OO design.

If your design is good, you should not find yourself needing to use getClass() or instanceof.

Any of the suggested methods will do, but just something to keep in mind, design-wise.

查看更多
登录 后发表回答