Class中isAssignableFrom() 方法

看Spring源码的时候看到这个方法:

1     protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
2         Class<?> contextClass = determineContextClass(sc);
3         if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
4             throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
5                     "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
6         }
7         return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
8     }

第一次见到第三行中的用法,点开看了下,原文是这么说的:

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false. 

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

就是说判断一个类是否另一个类或接口的子类。

例如

A.class.isAssignableFrom(B.class)

这句就是判断 B是否A的父接口或者父类。

顺手搜了下,得到一个小惊喜:

instanceof, isinstance,isAssignableFrom的区别

时间: 2024-08-05 00:42:15

Class中isAssignableFrom() 方法的相关文章

Java中isAssignableFrom()方法与instanceof()方法用法

一句话总结: isAssignableFrom()方法是从类继承的角度去判断,instanceof()方法是从实例继承的角度去判断. isAssignableFrom()方法是判断是否为某个类的父类,instanceof()方法是判断是否某个类的子类. 1. Class.isAssignableFrom()方法 Class.isAssignableFrom()是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的子类或接口.   格式为: Class1.isAssignable

Class的isAssignableFrom方法

Class类的isAssignableFrom是个不常用的方法,感觉这个方法的名字取得不是很好,所以有必要在此解析一下,以免在看源码时产生歧义,这个方法的签名如下: public native boolean isAssignableFrom(Class<?> cls); 由方法签名可见是一个本地方法,即C代码编写的. 以下是JDK中的注释: Determines if the class or interface represented by this Class object is eit

SSH配置环境都正常,但是每次执行到Action中的方法时就中断了,而且不报任何错误

SSH配置环境都正常,但是每次执行到Action中的方法时就不执行课,而且不报任何错误.Action中的方法封装的是业务层,业务层封装DAO层,检查了一天才发现错误在哪. 在applicationContext.xml中报了一个异常,如: Multiple annotations found at this line: - Exception 'com/dao/StudentDAO : Unsupported major.minor version 51.0' - Exception 'com/

Java中的方法重写 与 final关键字

Java中的方法重写 与 final关键字 以下程序是否存在错误? class Base { private final void method() { System.out.println("In Base..."); } } class Sub extends Base { public void method() { System.out.println("In Sub..."); } } 答案 正确的. 使用final关键字可以声明类.成员变量和成员方法,一

jsp 中声明方法的使用

1.在"<%!"和"%>"之间声明方法,该方法在整个JSP页面有效,但是该方法内定义的变量只在该方法内有效.这些方法将在Java程序片中被调用,当方法被调用时,方法内定义的变量被分配内存,调用完毕即可释放内存.当多个客户同时请求一个JSP页面时,他们可能使用方法操作成员变量,对这种情况应给予重视. 解决办法:通过synchronized方法操作一个成员变量来实现一个计数器 <%@ page contentType="text/html;c

JQuery中$.ajax()方法参数详解

url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 delete也可以使用,但仅部分浏览器支持. timeout: 要求为Number类型的参数,设置请求超时时间(毫秒).此设置将覆盖$.ajaxSetup()方法的全局设 置. async:要求为Boolean类型的参数,默认设置为true,所有请求均为异步请求. 如果需要发送同步请求,请将此选项

三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

版权声明:本文为sang原创文章,转载请注明出处. 目录(?)[+] 关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东.本篇博客我们不讲源码,只看使用.源码的解读会在下一篇博文中带来. inflate方法从大范围来看,分两种,三个参数的构造方法和两个参数的构造方法.在这两类中又有细分,OK,那我们就把各种情况都来演示一遍. 1.三个参数的in

ThinkPHP中create()方法自动验证

自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 原理: create()方法收集表单($_POST)信息并返回,同时触发表单自动验证,过滤非法字段, 在控制器中使用create()方法,(返回值为true/false),会自动触发模型类中的$_validate属性(为父类Model中的方法,在子类Model中重写),在$_validate中自定义验证规则(验证规则下面会详细说明),当create()方法没有数据即返回值为fals

Intent 中重要方法:setAction()、setData()、setType()、putExtra()

Intent大致分为两种:显式Intent和隐式Intent. 一.显示Intent为从一个activity跳转到另一个activity.如: Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); 二.隐式Intent它不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action和category等信息来过滤,找到符合条件的Activity. 1.setAc