子类和父类间可以相互强转吗?会出现什么问题?

chiphuo 2009-01-01 05:04:32
如题
...全文
2438 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2012-01-06
  • 打赏
  • 举报
回复
对我有用
a1b2c3zhang 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 createWang 的回复:]

举个例子:有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; // 出错,子类引用不能指向父类对象
[/Quote]

2楼说得非常好.
原则上父类和子类是可以互相转型的,所以编译的时候编译器不报错.
但是:父类在转型成子类的时候,时候条件限制的,如2楼的例子,就是父类对象所指向的实例是子类的实例.
wo554006164 2009-01-01
  • 打赏
  • 举报
回复
不错. 帮顶


bzwm 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chiphuo 的回复:]
bzwm

说的对,编译期间没有任何问题,在运行时候会出现类转换异常

再问个问题
Father father = new Son();
Father father = (Father)new son();

有什么区别?
[/Quote]

为什么要
Father father = (Father)new son(); 
呢?

没区别。如果 son 继承自Father,那么son 就是 Father。

我得买饭去啦,好冷啊~
chiphuo 2009-01-01
  • 打赏
  • 举报
回复
bzwm

说的对,编译期间没有任何问题,在运行时候会出现类转换异常

再问个问题
Father father = new Son();
Father father = (Father)new son();

有什么区别?
chenbo124 2009-01-01
  • 打赏
  • 举报
回复
向上转型 :父类引用执行子类,父类 fl = new 子类
向下转型: 子类 zl = (父类)fl 需要显示类型转换
bzwm 2009-01-01
  • 打赏
  • 举报
回复
当然可以正确运行了,

看我的注视:


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();
}
}
chiphuo 2009-01-01
  • 打赏
  • 举报
回复
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();
}
}


但这个程序运行完全正确啊
bzwm 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chiphuo 的回复:]
原理是这样的,为什么我的程序在编译的时候不会报错呢,还是说在运行期会出问题?
[/Quote]

子类转成父类,没有问题,
因为父类可见的接口子类一并继承了。

但父类转成子类可能会有问题了。
比如有 子类 teacher,继承父类 person,
person里有方法public String getSex(),
那么子类teacher一定有getSex方法的,要么自己重写了父类的,要么直接使用由父类继承的。

但如果你在程序中 person p = new person();
然后再 teacher t = (teacher)p;
就会有问题了,
因为 老师 肯定是 人,但,是人,不一定是老师。
createWang 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chiphuo 的回复:]
原理是这样的,为什么我的程序在编译的时候不会报错呢,还是说在运行期会出问题?
[/Quote]粘贴源码。
chiphuo 2009-01-01
  • 打赏
  • 举报
回复
原理是这样的,为什么我的程序在编译的时候不会报错呢,还是说在运行期会出问题?
hjiea666 2009-01-01
  • 打赏
  • 举报
回复
子类的对象可以强转为父类的对象,
但父类的不能强转为子类的
createWang 2009-01-01
  • 打赏
  • 举报
回复

举个例子:有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; // 出错,子类引用不能指向父类对象

kiss_the_java 2009-01-01
  • 打赏
  • 举报
回复
可以,也就是通常说的向上转型和向下转型。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧