See if two object have the same type

后端 未结 5 1817
终归单人心
终归单人心 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:33

    instanceof needs to point towards a class, not another object. To check that two objects both come from the same type of object do something to the following ..

    if((obj1 instanceof ClassName) && (obj2 instanceof ClassName)) {
      do whatever
    }
    

提交回复
热议问题