/**
* 注:
* 1.instanceof用法:实例变量 instanceof 类 (该类必须是实例变量的本类或者父类,否则编译失败,显示Inconvertible types)
* 2.而getClass(): return The Class object that represents the runtime class of this object.
* 用法:实例变量.getClass()==类.class ;如果想返回true,则实例变量必须是运行时指向对象*的实例。
* 3.==用于比较数值类型:即使数值类型不同,只要它们值相等,也会返回true。
* ==用于比较引用类型时,那么只有当两个引用变量的类型具有父子关系时才可以比较(否则会 报编译错误),而且这两个引用必须指向同一个对象才会返回true。
*/
class A{
public String toString(){
return “This is A Class!”;
}
}
class B extends A{
public String toString(){
return “This is B Class!”;
}
}
class Z{
public String toString(){
return “This is Class!”;
}
}
public class getClassTest {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
Z z = new Z();
System.out.println(“a1 instanceof A:”+(a1 instanceof A));//输出:a1 instanceof A:true
System.out.println(“a1.getClass()==A.Class:”+(a1.getClass()==A.class));//输出:a1.getClass()==A.Class:true
System.out.println(“a2 instanceof A:”+(a2 instanceof A));//输出:a2 instanceof A:true
System.out.println(“a2.getClass()==A.Class:”+(a2.getClass()==A.class));//输出:a2.getClass()==A.Class:false
System.out.println(a1==a2);//false
System.out.println(a2==b);//false
//System.out.println(a1==z); ==不能用于没有两个没有父子关系的引用变量
}
}