instanceof

Оператор для проверки приципа IS-A.
interface Domestic {}
class Animal {}
class Dog extends Animal implements Domestic {}
class Cat extends Animal implements Domestic {}
Imagine a dog object, created with Object dog = new Dog(), then:
dog instanceof Domestic // true - Dog implements Domestic
dog instanceof Animal   // true - Dog extends Animal
dog instanceof Dog      // true - Dog is Dog
dog instanceof Object   // true - Object is the parent type of all objects
However,
animal instanceof Dog // false
null instanceof Dog // false
And,
dog instanceof Cat // does not even compile!

Комментариев нет:

Отправить комментарий