您的位置:首页 > 编程语言 > Java开发

java isassignablefrom 判断子父类关系

2014-03-17 11:27 344 查看
java.lang.Class

详细见API.

/**
* Determines if the class or interface represented by this
* {@code Class} object is either the same as, or is a superclass or
* superinterface of, the class or interface represented by the specified
* {@code Class} parameter. It returns {@code true} if so;
* otherwise it returns {@code false}. If this {@code Class}
* object represents a primitive type, this method returns
* {@code true} if the specified {@code Class} parameter is
* exactly this {@code Class} object; otherwise it returns
* {@code false}.
*
* <p> Specifically, this method tests whether the type represented by the
* specified {@code Class} parameter can be converted to the type
* represented by this {@code Class} object via an identity conversion
* or via a widening reference conversion. See <em>The Java Language
* Specification</em>, sections 5.1.1 and 5.1.4 , for details.
*
* @param cls the {@code Class} object to be checked
* @return the {@code boolean} value indicating whether objects of the
* type {@code cls} can be assigned to objects of this class
* @exception NullPointerException if the specified Class parameter is
*            null.
* @since JDK1.1
*/
public native boolean isAssignableFrom(Class<?> cls);


java 文档说明。

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.

代码使用

public class AssingableTest {
public static void main(String[] args) {
Class<?> parent = java.io.InputStream.class;
Class<?> child = java.io.FileInputStream.class;
System.out.println(parent.isAssignableFrom(child));
}
}


可以用来校验一个类是否实现指定的父类。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: