
本文共 3527 字,大约阅读时间需要 11 分钟。
������
���������Java���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������
������������������������������������������������������������������������������������������������
���������������Upcasting���
���������������������������������������������������������������������Java������������������������������������������������������������������������������������������������������������������������Animal a = new Cat(); // ���������������������������������Cat���Animal���������a.move(); // ������������������a������������Cat���������������������������Cat���move������
������������������������������������������������������������
���������������Downcasting���
��������������������������������������������������������������������������� programmer ������������������������������ClassCastException������������Cat c = (Cat)a; // ���������������������c.catchMouse(); // ���������������Cat���������
���������������������������������������������������������������������������������������������������������������������
Java���������������������
Java���������������������phase������������������������������������������������
���������������Static Binding���
���������������������������������������������������������������������������������������������Animal a1 = new Cat(); // ������������������a1������������Animal���������������������������
������Cat���������������Animal���������������������������������������������������������������������������������������
���������������Dynamic Binding���
������������������JVM���������������������������������������������������������a1.move(); // ������������������Cat������move������
������������������������������������������������������������������������������
������������������������
������������������������������������������������������������������������������������������
// ������Cat������catchMouse������if (a instanceof Cat) { Cat c = (Cat)a; c.catchMouse();}
���������������������������������������������instanceof���������������������������������������������������������������������ClassCastException���
���������������������������������
������������������������������������������������������instanceof���������������������������������������������
Object obj = new String(); // String���Object���������if (obj instanceof String) { String s = (String)obj; System.out.println("True"); // ������������} else { System.out.println("False"); // ������������������������String}
������������������������������������������������������������
instanceof���������
instanceof���������������������������������������������������������������������������������������������true���false������������
boolean isBird = a3 instanceof Bird; // true������a3������������Bird������������
������������������������������������������������������������������������
������
���������������������������������������������������������������������������������������������������������Java������������������������������������������������������������������������������������������������������������������������������������������������������������������������������instanceof���������������������������
���������������������������������������������������������������������������������������������������������������������Java���������������������������������������������������������������������
发表评论
最新留言
关于作者
