举个例子:有2个类,Father是父类,Son类继承自Father。Father f1 = new Son(); // 这就叫 upcasting (向上转型)// 现在f1引用指向一个Son对象Son s1 = (Son)f1; // 这就叫 downcasting (向下转型)// 现在f1还是指向Son对象第2个例子:Father f2 = new Father();Son s2 = (Son)f2; // 出错,子类引用不能指向父类对象
子类转成父类,没有问题, 因为父类可见的接口子类一并继承了。但父类转成子类可能会有问题了。 比如有 子类 teacher,继承父类 person, person里有方法public String getSex(), 那么子类teacher一定有getSex方法的,要么自己重写了父类的,要么直接使用由父类继承的。但如果你在程序中 person p = new person(); 然后再 teacher t = (teacher)p; 就会有问题了, 因为 老师 肯定是 人,但,是人,不一定是老师。
package test;// codes start class base {// a base class // constructor public base() { System.out.println("base class construct"); } // perform public void perform() { System.out.println("base class perform"); }} class subbase extends base {// derive from base // constructor public subbase() { System.out.println("sub class construct"); } // perform public void perform() { System.out.println("sub class perform"); }}public class casting {// test casting class // constructor public casting() { System.out.println("begin casting test"); } public static void main(String args[]) { base father = new base(); subbase son = new subbase(); System.out.println("---------------------"); father = son; // <1> father.perform(); son = (subbase)father; // <2> son.perform(); father = (base) ((subbase) father); // <3> father.perform(); System.out.println("------------------------");
((base)new subbase()).perform(); } }但这个程序运行完全正确啊
当然可以正确运行了,看我的注视: class base {// a base class // constructor public base() { System.out.println("base class construct"); } // perform public void perform() { System.out.println("base class perform"); }} class subbase extends base {// derive from base // constructor public subbase() { System.out.println("sub class construct"); } // perform public void perform() { System.out.println("sub class perform"); }}public class casting {// test casting class // constructor public casting() { System.out.println("begin casting test"); } public static void main(String args[]) { base father = new base(); subbase son = new subbase(); System.out.println("---------------------"); father = son; // <1> 你在这里把father指向了 son,也就son指向的new subbase(); //此时的 father和son 其实是相同的了。如果你把这句去掉,你在看看 <2>会不会爆掉。 father.perform(); son = (subbase)father; // <2> son.perform(); father = (base) ((subbase) father); // <3> father.perform(); System.out.println("------------------------");
举个例子:有2个类,Father是父类,Son类继承自Father。Father f1 = new Son(); // 这就叫 upcasting (向上转型)// 现在f1引用指向一个Son对象Son s1 = (Son)f1; // 这就叫 downcasting (向下转型)// 现在f1还是指向Son对象第2个例子:Father f2 = new Father();Son s2 = (Son)f2; // 出错,子类引用不能指向父类对象
子类转成父类,没有问题,
因为父类可见的接口子类一并继承了。但父类转成子类可能会有问题了。
比如有 子类 teacher,继承父类 person,
person里有方法public String getSex(),
那么子类teacher一定有getSex方法的,要么自己重写了父类的,要么直接使用由父类继承的。但如果你在程序中 person p = new person();
然后再 teacher t = (teacher)p;
就会有问题了,
因为 老师 肯定是 人,但,是人,不一定是老师。
class base {// a base class // constructor
public base() {
System.out.println("base class construct");
} // perform
public void perform() {
System.out.println("base class perform");
}}
class subbase extends base {// derive from base // constructor
public subbase() {
System.out.println("sub class construct");
} // perform
public void perform() {
System.out.println("sub class perform");
}}public class casting {// test casting class
// constructor
public casting() {
System.out.println("begin casting test");
} public static void main(String args[]) {
base father = new base();
subbase son = new subbase();
System.out.println("---------------------");
father = son; // <1>
father.perform(); son = (subbase)father; // <2>
son.perform(); father = (base) ((subbase) father); // <3>
father.perform();
System.out.println("------------------------");
((base)new subbase()).perform();
}
}但这个程序运行完全正确啊
class base {// a base class // constructor
public base() {
System.out.println("base class construct");
} // perform
public void perform() {
System.out.println("base class perform");
}}
class subbase extends base {// derive from base // constructor
public subbase() {
System.out.println("sub class construct");
} // perform
public void perform() {
System.out.println("sub class perform");
}}public class casting {// test casting class
// constructor
public casting() {
System.out.println("begin casting test");
} public static void main(String args[]) {
base father = new base();
subbase son = new subbase();
System.out.println("---------------------");
father = son; // <1> 你在这里把father指向了 son,也就son指向的new subbase();
//此时的 father和son 其实是相同的了。如果你把这句去掉,你在看看 <2>会不会爆掉。
father.perform(); son = (subbase)father; // <2>
son.perform(); father = (base) ((subbase) father); // <3>
father.perform();
System.out.println("------------------------");
((base)new subbase()).perform();
}
}
向下转型: 子类 zl = (父类)fl 需要显示类型转换
Father father = new Son();
Father father = (Father)new son();有什么区别?
为什么要 Father father = (Father)new son(); 呢?没区别。如果 son 继承自Father,那么son 就是 Father。我得买饭去啦,好冷啊~