Ebben az oktatóanyagban példák segítségével részletesen megismerheti a Java instanceof operátort.
A instanceof
Java operátor segítségével ellenőrizhető, hogy egy objektum egy adott osztály példánya-e vagy sem.
Szintaxisa az
objectName instanceOf className;
Itt, ha az objectName a className példánya, az operátor visszatér true
. Ellenkező esetben visszatér false
.
Példa: 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); ) )
Kimenet
A név a String: true objektuma a Main: true példánya
A fenti példában létrehoztunk egy változó nevű String
típust és egy objektum objektumot a Main osztályból.
Itt az instanceof
operátort használtuk annak ellenőrzésére, hogy a név és az objekt a, String
illetve a Fő osztály példányai . És az operátor true
mindkét esetben visszatér .
Megjegyzés : A Java-ban String
inkább osztály, mint primitív adattípus. További információkért látogasson el a Java karakterláncra.
Java instanceof az öröklés során
Az instanceof
operátor segítségével ellenőrizhetjük, hogy az alosztály objektumai is a szuperosztály példányai. Például,
// 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 ) )
A fenti példában létrehoztunk egy alosztályos kutyát, amely a szuperosztályú állattól örököl. Hoztunk létre egy kutya osztály d1 objektumát.
A nyomtatott utasításban vegye észre a kifejezést,
d1 instanceof Animal
Itt az instanceof
operátor segítségével ellenőrizzük, hogy a d1 is a szuperosztályú Animal példánya.
Java instanceof az interfészben
Az instanceof
operátort arra is ellenőrizzük, hogy egy osztály objektuma egyben az osztály által megvalósított interfész példánya is. Például,
// 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 ) )
A fenti példában a Dog osztály megvalósítja az Animal interfészt. A nyomtatott utasításban vegye észre a kifejezést,
d1 instanceof Animal
Itt a d1 a Dog osztály példánya. Az instanceof
operátor ellenőrzi, hogy a d1 is az Animal interfész példánya.
Megjegyzés : A Java-ban az összes osztály öröklődik az Object
osztálytól. Tehát az összes osztály példánya az Object
osztály példánya is .
Az előző példában, ha ellenőrizzük,
d1 instanceof Object
Az eredmény az lesz true
.