Java中的instanceOf
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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。
