zoukankan      html  css  js  c++  java
  • 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的区别

  • 相关阅读:
    从数据库表中查询日期最新的记录
    ArcGIS js api开发环境配置
    HRESULT:0x80070057 (E_INVALIDARG)
    ArcGIS js api三种查询功能
    sql设置字段默认值
    文件后缀与mime类型对应表
    关于dojo自定义类
    android用户登录验证
    java实现QQ互联登录
    springboot实现网站微信扫码登录
  • 原文地址:https://www.cnblogs.com/larryzeal/p/5885166.html
Copyright © 2011-2022 走看看