Java類class isAssignableFrom()方法及示例

類class isAssignableFrom()方法 (Class class isAssignableFrom() method)

  • isAssignableFrom() method is available in java.lang package.

    isAssignableFrom()方法在java.lang包中可用。

  • isAssignableFrom() method is used to check whether the class or an interface denoted by this Class object is either the same as, or the Class object is a superclass or superinterface.

    isAssignableFrom()方法用于檢查此Class對象所表示的類或接口是否與該類或接口相同,或者該Class對象是超類還是超接口。

  • isAssignableFrom() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    isAssignableFrom()方法是一個非靜態方法,只能使用類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。

  • isAssignableFrom() method may throw an exception at the time of assigning an object.

    在分配對象時, isAssignableFrom()方法可能會引發異常。

    NullPointerException: In the exception, when the given class exists null.

    NullPointerException :在異常中,當給定的類存在時為null。

Syntax:

句法:

    public boolean isAssignableFrom(Class class);

Parameter(s):

參數:

  • Class class – represents the Class object to be determined.

    類class –表示要確定的Class對象。

Return value:

返回值:

The return type of this method is boolean, it returns a boolean value based on the following cases,

此方法的返回類型為boolean ,它基于以下情況返回布爾值:

  • It returns true, when the object of class is assignable to object of this Class.

    當類的對象可分配給該類的對象時,它返回true

  • It returns false, when the object of class is not assignable to object of this Class.

    當class的對象不可分配給該Class的對象時,它返回false

Example:

例:

// Java program to demonstrate the example 
// of boolean isAssignableFrom(Class class) method of Class 
public class Parent {
public static void main(String[] args) throws Exception {
// Create and Return Parent Class object
Parent p = new Parent();
Class cl1 = p.getClass();
// Create and Return Child Class object
Child ch = new Child();
Class cl2 = ch.getClass();
// We are checking the given Parent class is 
// Assignable from Child Class
boolean child = cl2.isAssignableFrom(cl1);
System.out.println("Is" + " " + cl1.getSimpleName() + " " + "Assignable from Child: " + " " + child);
// We are checking the given Child class is 
// Assignable from Parent Class
boolean parent = cl1.isAssignableFrom(cl2);
System.out.println("Is" + " " + cl2.getSimpleName() + " " + "Assignable from Parent: " + " " + parent);
}
}
class Child extends Parent {
public Child() {
// Default Constructor with blank implementation
}
}

Output

輸出量

Is Parent Assignable from Child:  false
Is Child Assignable from Parent:  true

翻譯自: https://www.includehelp.com/java/class-class-isassignablefrom-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379729.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379729.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379729.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

關于 列表實例

wss3.0工具中有個列表實例項目。此項目的作用是在自定義網站或自定義字段時使用默認值。也就是定義其默認的數據。 格式詳見微軟msdn:http://msdn.microsoft.com/zh-cn/library/ms478860.aspx轉載于:https://www.cnblogs.com/heavencloud/archive/2009/03/20/141793…

WP7之Application Bar控件

Microsoft.Phone.Shell命名空間中定義了ApplicationBar及其相關類(ApplicationBarIconButton和ApplicationBarMenuItem),這些類派生自Object,并完全獨立于常規Silverlight編程中的DependencyObject,UIElement和FrameworkElement類層次結構。A…

TomCat使用以及端口號被占用的處理方法

一.HTTP協議 什么是HTTP協議 HTTP協議(HyperText Transfer Protocol,超文本傳輸協議)是因特網上應用最為廣泛的一種網絡傳輸協議,所有的WWW文件都必須遵守這個標準。 HTTP請求 HTTP響應 2.如何處理端口被占用 方法一&#xff…

FreeRTOS事件標志組

使用信號量來同步的話,任務只能與單個事務或任務進行同步,有時候某個任務可能會需要與多個事件或任務進行同步,此時信號量就無能為力了,FreeRTOS為此提供了一個可選的解決方法,那就是事件標志組。 0x01 事件標志組 事…

FusionCharts等產品簡介

以前做柱狀圖、餅形圖等圖表都是根據數據繪制出來的靜態圖,偶然看到別人的一套系統,居然可以讓柱狀圖的柱子動畫般的逐個出現,效果還是很不錯的。不要跟我抬杠說不就是展現數據嘛,靜態圖表有什么不好,要知道用戶一般可…

c#foreach循環_C#| 使用foreach循環打印整數數組

c#foreach循環Given an integer array and we have to print its elements using "foreach loop" in C#. 給定一個整數數組,我們必須在C#中使用“ foreach循環”打印其元素 。 Syntax for foreach loop: foreach循環的語法: fore…

Eclipse和Tomcat綁定并且將上傳資源到Tomcat上

步驟如下: 創建一個Dynamic Web Project(圖一) Target runtime 選擇Apache Tomcat v7.0版本(圖二) 切記要選擇 v7.0 和2.5 (若沒有圖二選項見圖三) 然后,點擊window --> Prefer…

淺析.NET平臺編程語言的未來走向

在去年的PDC2008召開期間,微軟逐步公開了圍繞.NET和編程語言的很多想法,據此我們可以饒有興趣地對.NET的未來預測一番。 .NET平臺以運行在通用語言運行時(Common Language Runtime,CLR)上的C#和VB.NET作為開端。CLR是通用語言架構(Common Lan…

FreeRTOS任務通知

從v8.2.0版本開始,FreeRTOS新增了任務通知這個功能,可以使用任務通知來代替信號量、消息隊列、事件標志組等這些東西,使用任務通知的話效率會更高。 任務通知在FreeRTOS是一個可選的選項,要使用任務通知的話就需要將宏configUSE_T…

kinect在openni下也能玩摳出人物換背景

之前想了個很拉風的名字《用kinect玩穿越》,但是現在功能還不是很完善,細節處理也不是很好,臉皮沒有足夠的厚,所以呢還是叫換背景吧。 這里面包含兩個技術要點: 一、摳出活動人物 在微軟的SDK里深度圖像的前3位即0-2位…

物聯網基礎知識_聯網| 基礎知識能力問答 套裝1

物聯網基礎知識1) There are the following statements that are given below, which of them are correct about the computer network? A computer network is an interconnection between multiple devices to share hardware resources and information.A computer networ…

Emit學習-基礎篇-基本概念介紹

之前的Hello World例子應該已經讓我們對Emit有了一個模糊的了解,那么Emit到底是什么樣一個東西,他又能實現些什么功能呢?昨天查了點資料,大致總結了下,由于才開始學習肯定有不完善的地方,希望大家能夠批評指…

The FreeRTOS Distribution(介紹、移植、類型定義)

1 Understand the FreeRTOS Distribution 1.1 Definition :FreeRTOS Port FreeRTOS目前可以在20種不同的編譯器構建,并且可以在30多種不同的處理器架構上運行,每個受支持的編譯器和處理器組合被認為是一個單獨的FreeRTOS Port。 1.2 Build…

notepad++節點_在C ++中刪除鏈接列表的中間節點

notepad節點Given a single Linked List and we have to delete the middle the element of the Linked List. 給定一個鏈表,我們必須刪除鏈表中間的元素。 If the length of the linked list is odd then delete (( n1)/2)th term of the linked list and if the…

SET ANSI_NULLS ON

指定在與 Null 值一起使用等于 () 和不等于 (<>) 比較運算符時采用符合 ISO 標準的行為。 當 SET ANSI_NULLS 為 ON 時&#xff0c;即使 column_name 中包含空值&#xff0c;使用 WHERE column_name NULL 的 SELECT 語句仍返回零行。即使 column_name 中包含非空值&…

Eclipse項目左上角出現大紅色感嘆號怎么辦?

出現大紅色感嘆號是因為環境不匹配 解決方法&#xff1a; 右擊出現大紅色感嘆號的項目 點擊 Libraries&#xff0c;將有叉號的給Remove掉 然后再點擊 Add Library —> JRE System Library —> Next 勾選第二個即可 之后&#xff0c;就不會出現大紅色感嘆號了。

PCB---STM32最小系統制作過程

PCB 制作過程STM32核心模塊連接外部電源晶振OSC_IN(8MHz)OSC32_IN(32.768MHz&#xff09;復位下載口BOOT模式電源模塊添加功能UARTWKUPSTM32核心模塊 這里我們以STM32F103C8T6為列&#xff0c;先將芯片的原理圖放到原理圖中 對于STM32&#xff0c;有幾個模塊是核心&#xff0…

scala 隨機生成整數_如何在Scala中以整數形式獲取當前年份?

scala 隨機生成整數In Scala programming language, there is an option for the programmer to use libraries of java because of its interoperability with java. 在Scala編程語言中&#xff0c;程序員可以選擇使用Java庫&#xff0c;因為它可以與Java互操作。 There are …

轉載:glut.h 與 stdlib.h中 的exit()重定義問題的解決

遇到的問題&#xff0c;來自&#xff1a;http://blog.sina.com.cn/s/blog_629c53bd0100f5li.html 出現&#xff1a; c:\codeprogram\microsoft visual studio 10.0\vc\include\stdlib.h(353): error C2381: “exit”: 重定義&#xff1b;__declspec(noreturn) 不同1> c:\pro…

括號配對問題(C++棧)

題目描述: 現在&#xff0c;有一行括號序列&#xff0c;請你檢查這行括號是否配對。 輸入描述: 第一行輸入一個數N&#xff08;0<N<100&#xff09;,表示有N組測試數據。后面的N行輸入多組輸入數據&#xff0c;每組輸入數據都是一個字符串S(S的長度小于10000&#xff0c;…