Java instanceof (z przykładami)

W tym samouczku za pomocą przykładów dowiesz się szczegółowo o operatorze instancji Java.

instanceofOperator w Javie służy do sprawdzania, czy obiekt jest instancją danej klasy, czy nie.

Jego składnia to

 objectName instanceOf className;

Tutaj, jeśli objectName jest instancją className, operator zwraca true. W przeciwnym razie wraca false.

Przykład: Java instanceof

 class Main ( public static void main(String() args) ( // create a variable of string type String name = "Programiz"; // checks if name is instance of String boolean result1 = name instanceof String; System.out.println("name is an instance of String: " + result1); // create an object of Main Main obj = new Main(); // checks if obj is an instance of Main boolean result2 = obj instanceof Main; System.out.println("obj is an instance of Main: " + result2); ) )

Wynik

 name jest instancją String: true obj jest instancją Main: true

W powyższym przykładzie utworzyliśmy nazwę zmiennej Stringtypu oraz obiekt obj klasy Main.

Tutaj użyliśmy instanceofoperatora, aby sprawdzić, czy name i obj są odpowiednio instancjami Stringklas i Main. W trueobu przypadkach operator zwraca .

Uwaga : w Javie Stringjest to klasa, a nie prymitywny typ danych. Aby dowiedzieć się więcej, odwiedź witrynę Java String.

Instancja Java podczas dziedziczenia

Możemy użyć instanceofoperatora, aby sprawdzić, czy obiekty podklasy są również instancją nadklasy. Na przykład,

 // Java Program to check if an object of the subclass // is also an instance of the superclass // superclass class Animal ( ) // subclass class Dog extends Animal ( ) class Main ( public static void main(String() args) ( // create an object of the subclass Dog d1 = new Dog(); // checks if d1 is an instance of the subclass System.out.println(d1 instanceof Dog); // prints true // checks if d1 is an instance of the superclass System.out.println(d1 instanceof Animal); // prints true ) )

W powyższym przykładzie utworzyliśmy podklasę Dog, która dziedziczy po nadklasie Animal. Stworzyliśmy obiekt d1 klasy Dog.

W instrukcji print zwróć uwagę na wyrażenie:

 d1 instanceof Animal

Tutaj używamy instanceofoperatora, aby sprawdzić, czy d1 jest również instancją superklasy Animal.

Instancja Java w interfejsie

instanceofOperator jest także stosowane do sprawdzenia, czy obiekt klasy jest również przykład interfejsu realizowanego przez klasę. Na przykład,

 // Java program to check if an object of a class is also // an instance of the interface implemented by the class interface Animal ( ) class Dog implements Animal ( ) class Main ( public static void main(String() args) ( // create an object of the Dog class Dog d1 = new Dog(); // checks if the object of Dog // is also an instance of Animal System.out.println(d1 instanceof Animal); // returns true ) )

W powyższym przykładzie klasa Dog implementuje interfejs Animal. W instrukcji print zwróć uwagę na wyrażenie:

 d1 instanceof Animal

Tutaj d1 jest instancją klasy Dog. Te instanceofkontrole operatora jeśli d1 jest także instancją Animal interfejsu.

Uwaga : w Javie wszystkie klasy są dziedziczone z Objectklasy. Tak więc wystąpienia wszystkich klas są również instancjami Objectklasy.

W poprzednim przykładzie, jeśli sprawdzimy,

 d1 instanceof Object

Wynik będzie true.

Interesujące artykuły...