标签:lse signed pen int cas lang testcase 调用 []
假设有两个类Class1和Class2。Class1.isAssignableFrom(Class2)
表示:
用来判断一个对象是否是一个类及其子类或接口及其子接口的的实例。
格式:object instanceof TypeName
第一个参数是对象实例名,第二个参数是具体的类名或接口名
public class TestCase {
public static void main(String[] args) {
TestCase test = new TestCase();
test.testIsAssignedFrom1();
test.testIsAssignedFrom2();
test.testIsAssignedFrom3();
test.testInstanceOf1();
test.testInstanceOf2();
}
public void testIsAssignedFrom1() {
System.out.println(String.class.isAssignableFrom(Object.class));
}
public void testIsAssignedFrom2() {
System.out.println(Object.class.isAssignableFrom(Object.class));
}
public void testIsAssignedFrom3() {
System.out.println(Object.class.isAssignableFrom(String.class));
}
public void testInstanceOf1() {
String ss = "";
System.out.println(ss instanceof Object);
}
public void testInstanceOf2() {
Object o = new Object();
System.out.println(o instanceof Object);
}
}
打印结果:
false
true
true
true
true
参考连接:
https://lucky16.iteye.com/blog/1631253
Class.isAssignableFrom与instanceof的区别
标签:lse signed pen int cas lang testcase 调用 []
原文地址:https://www.cnblogs.com/wangkaihua/p/10339421.html