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.mzph.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;…