首頁  >  文章  >  Java  >  Java中的instanceOf

Java中的instanceOf

WBOY
WBOY原創
2024-08-30 15:44:53888瀏覽

Java中的InstanceOf用於在Java程式碼片段中實作繼承概念時確定物件與其類別的關係。一般來說,instanceOf類別適用於允許繼承的物件導向程式語言,它以布林結果的形式傳回輸出值,即true或false。如果 instanceOf 類別變數具有 NULL 值,則該類別將輸出傳回為「false」。由於此類用於比較目的,因此也稱為“類型比較運算符”。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

instanceOf 類別用於檢查物件是否屬於任何類別。

obj instanceOf object

以上是instanceOf類的標準語法。這裡, obj 是之前建立的物件的名稱(引用)。 instanceOf 是關鍵字,object 是我們與 obj 物件相符的類別或介面。

在各種情況下,instanceOf可以被證明是有主要用途的,例如我們有一個物件集合,而你不確定它引用的是哪個物件。這種情況的一個例子是帶有許多控制項的簡單表單。

此外,如果我們將 instanceOf 與具有 NULL 值的變數一起使用,它肯定會傳回 false。

實例如何運作?

java中的instanceOf運算子適用於簡單的is-a關係。簡單地說,is-a 關係是一個物件導向的概念,我們在抽象之間比較或說處理抽象之間的關係,其中類別 A 是類別 B 的子類別。這是一種完全基於繼承的關係。換句話說,這就像在說「X是Y類型」。

現在,讓我們來了解 instanceOf 的工作原理以及相應的程式碼。

首先,我們將建立一個名為 Parent 的類別。

代碼:

class Parent{
}
//Then let’s add a simple main class.
public static void main(String args[]) {
}

然後我們將建立 Parent 類別的一個實例。

Parent child = new Parent();

最後,我們將使用instanceOf運算子來檢查child和Parent之間的關係。如下圖所示: child instanceOf Parent

現在,如前所述,instanceOf 的語法來自於必須檢查的對象,然後是instanceOf 關鍵字,然後是要測試給定對象的類別/介面。

在類別聲明中遇到關鍵字「extends」或「implements」的任何時候,這都是正在使用 is-a 關係的明顯標誌。

Java中instanceOf的例子

以下範例示範了instanceOf的單行使用。

class instanceof_java{
public static void main(String args[]) {
instanceof_java s = new instanceof_java();
System.out.println(s instanceOf instanceof_java);
}
}

程式碼解讀:從建立一個簡單的類別instanceof_java開始。在類instanceof_java中,我們有我們的主類,並且在我們的主類中,我們建立了一個物件。這個物件s是instanceof_java類型的。然後為了實現instanceOf的工作,我們提供了一個帶有物件s的輸出語句。在最後一行中,我們將 s 與 instanceof 關鍵字和父類別一起傳遞。執行後,程式碼將傳回“true”,因為物件 s 是 instanceof 類型。

Java中的instanceOf

更進一步,如果我們有一個已知類或介面的對象,但沒有為同一個對象分配任何值,它必然會返回 false,即使我們屬於同一類。

class instanceof_sample{
public static void main(String args[]) {
instanceof_sample new = null;
System.out.println(new instanceOf instanceof_sample);
}
}

這裡我們有一個與前面的範例類似的程式碼,但具有空值物件。執行時,此程式碼將傳回 false。正如我們所看到的,物件new是instanceof_sample的實例,但new被分配了一個空值,並傳回false。

Java中的instanceOf

instanceOf 運算子的規則

基於物件 ref 是否不為 null 以及引用型別的實例。當 X 是所引用物件的簡單類,Y 是已解析的類別或介面類型的陣列時。

  • When X is a simple class, then:
  • If Y is a class type, then the X must be a subclass of Y, or X must the same class as Y.
  • If Y is an interface type, then the X class must implement interface T.
  • When X is type interface, then:
  • If Y is a class type, then the Y must be an Object.
  • Y can be the same as the interface as X or super interface of X if Y is an interface type.
  • If X is a class, which is representing the array type SC[], which is an array of type SC components, then:
  • If Y is a class type, then Y must be an object.
  • If Y is an interface type, then Y must be of interface type implemented by arrays.

Finally, we will demonstrate an instanceOf program to understand that the parent object cannot be an instance of the child class.

Program

class Subject {  }
class Topic extends Subject { }
class instanceof_java
{
public static void main(String[] args)
{
Subject history = new Subject ();
if (history instanceof Topic)
System.out.println("history is instance of Topic");
else
System.out.println("history is NOT instance of Topic");
}
}

Code Interpretation: For the purpose of understanding the instanceOf operator in different scenarios, we wrote the above code. We created a simple class Subject and then another class Topic, which extends class Subject, making the class Topic as child and class Subject as Parent here. Then another class with the main method. Within the main method, we created a new instance of parent class Subject. In the IF ELSE loop, we checked the instance object’s condition with the parent class Subject. If the condition was fulfilled, it would return “history is an instance of Topic” and “history is NOT an instance of Topic” if the condition fails.

Upon executing the above code, the output will be “history is NOT an instance of Topic”, meaning the condition passed in IF fails. It happened because we tried to check the parent of the object “history” with the class Topic. We know that the class Topic extends the class Subject, meaning Topic is a subclass to Subject. And when we tried to check the type of history with class Topic, it returns false (NOT). This means the Parent Object cannot be an instance of a class.

 Output:

Java中的instanceOf

Conclusion

We have learned about instanceOf class in Java, which simply decides if the object is of the given type. We understood how is-a relationship impacts on instanceOf operator. Also known as a comparison operator, instanceOf is based on inheritance.

以上是Java中的instanceOf的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn