See if two object have the same type

后端 未结 5 1818
终归单人心
终归单人心 2021-02-04 02:00

Let\'s say that I have a class A, and that B,C,D are derived from A.
If I want to know what\'s the type of an object referenced, I can declare:

// pseudo-c         


        
5条回答
  •  余生分开走
    2021-02-04 02:37

    Since B, C and D are subclasses of A, and you want to do something with these classes, I wouldn't use the instanceOf operator. This one's only useful for when there's no other way.

    You could better override the super methods and/or variables, so you can use all the objects the same, tough they will do something different (for example printing it's own type).

提交回复
热议问题