Comparing two classes by its types or class names

前端 未结 7 1096
耶瑟儿~
耶瑟儿~ 2021-02-01 15:34

There is need to compare two objects based on class they implement? When to compare using getClass() and when getClass().getName()? Is there any differ

7条回答
  •  执念已碎
    2021-02-01 15:40

    As a matter of fact comparing trusted class by name is a weakness. Refer https://cwe.mitre.org/data/definitions/486.html

    if (nextMonster.getClass() == monster.getClass())
    

    is the correct way to compare classes

    if (nextMonster.getClass().equals(monster.getClass()))
    

    should still work for the same reason @Bohemian mentioned

提交回复
热议问题