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 類型。
更進一步,如果我們有一個已知類或介面的對象,但沒有為同一個對象分配任何值,它必然會返回 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。
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:
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中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。