Java determine which class an object is

前端 未结 6 1744
悲&欢浪女
悲&欢浪女 2021-01-26 20:36

I have three classes (Carnivore, Herbivore, and Plant) that extend another class (Organism). How can I tell which subclass an

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 20:56

    Take a look at instanceof operator

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

    Note that although many people thinks that using it may be considered dangerous, they even compare to GOTO, but it's not bad in some cases. You can use it, but not really often.

提交回复
热议问题