Class中isAssignableFrom() 方法
生活随笔
收集整理的這篇文章主要介紹了
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的區別
?
轉載于:https://www.cnblogs.com/larryzeal/p/5885166.html
總結
以上是生活随笔為你收集整理的Class中isAssignableFrom() 方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛排杯大概多少钱一杯?
- 下一篇: OC基本数据存储方式