Detaillierte Erläuterung der Verwendungsinstanz in Java
Jun 29, 2017 am 11:34 AMInstanceof ist ein binärer Operator (Operator) in Java und auch ein reserviertes Schlüsselwort in Java. Seine Funktion besteht darin, zu bestimmen, ob das Objekt auf der linken Seite eine Instanz der Klasse auf der rechten Seite ist, und boolesche Daten zurückzugeben. Verwenden Sie es, um zu bestimmen, ob ein Objekt eine Instanz einer Class-Klasse ist.
Verwendung:
boolean result = object instanceof class
Parameter:
Ergebnis: boolescher Typ.
Objekt: erforderlich. Beliebiger Objektausdruck.
Klasse: erforderlich. Jede definierte Objektklasse.
Erklärung:
Wenn das Objekt eine Instanz der Klasse ist, dann wird „true“ zurückgegeben. Wenn das Objekt keine Instanz dieser Klasse ist oder das Objekt null ist, wird „false“ zurückgegeben.
Beispiel:
package com.instanceoftest; interface A { } class B implements A { } //B是A的实现 class C extends B { } //C继承B class D { } class instanceoftest { public static void main(String[] args) { A a = null; B b = null; boolean res; System.out.println("instanceoftest test case 1: ------------------"); res = a instanceof A; System.out.println("a instanceof A: " + res); // a instanceof A:false res = b instanceof B; System.out.println("b instanceof B: " + res); // b instanceof B: false System.out.println("\ninstanceoftest test case 2: ------------------"); a = new B(); b = new B(); res = a instanceof A; System.out.println("a instanceof A: " + res); // a instanceof A:true res = a instanceof B; System.out.println("a instanceof B: " + res); // a instanceof B:true res = b instanceof A; System.out.println("b instanceof A: " + res); // b instanceof A:true res = b instanceof B; System.out.println("b instanceof B: " + res); // b instanceof B:true System.out.println("\ninstanceoftest test case 3: ------------------"); B b2 = new C(); res = b2 instanceof A; System.out.println("b2 instanceof A: " + res); // b2 instanceof A:true res = b2 instanceof B; System.out.println("b2 instanceof B: " + res); // b2 instanceof A:true res = b2 instanceof C; System.out.println("b2 instanceof C: " + res); // b2 instanceof A:true System.out.println("\ninstanceoftest test case 4: ------------------"); D d = new D(); res = d instanceof A; System.out.println("d instanceof A: " + res); // d instanceof A:false res = d instanceof B; System.out.println("d instanceof B: " + res); // d instanceof B:false res = d instanceof C; System.out.println("d instanceof C: " + res); // d instanceof C:false res = d instanceof D; System.out.println("d instanceof D: " + res); // d instanceof D:true } }
Das obige ist der detaillierte Inhalt vonDetaillierte Erläuterung der Verwendungsinstanz in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Brechen oder aus Java 8 Stream foreach zurückkehren?
