Topic: 关于对象在继承关系中的转变的问题。

  Print this page

1.关于对象在继承关系中的转变的问题。 Copy to clipboard
Posted by: caoguanghui
Posted on: 2006-03-18 21:39

程序如下:
class SuperClass{
private int x=100;
public int getX() {
return x;
}
}
class Subbie extends SuperClass{
private int y=200;
public int getY(){
return y;
}
}
public class ObjectConvert{
public static void main(String[] arg){
SuperClass superA = new SuperClass(),superB;
Subbie subA=new Subbie(),subB;
(new ObjectConvert()).useSubAsSuper(subA);
superB=subA;
System.out.println("superB.getX: "+superB.getX());
subB=(Subbie)superB;
System.out.println(subB.getX+" "+subB.getY()); //编译的时候显示这行出错 提示为 cannot resolve symbol.
}
public void useSubAsSuper(SuperClass x){
System.out.println(x.getX()+"!!!!!!");
}
}
难道是 subB=(Subbie)superB; 一行出错?但是书上讲可以将指向子类对象地址的父类对象强制转换成子类对象的啊!

2.Re:关于对象在继承关系中的转变的问题。 [Re: caoguanghui] Copy to clipboard
Posted by: caoguanghui
Posted on: 2006-03-19 15:51

没有热心人吗?
小弟真的是思考很就也不得其解 才放上来的啊!

3.Re:关于对象在继承关系中的转变的问题。 [Re: caoguanghui] Copy to clipboard
Posted by: Skybus
Posted on: 2006-03-19 18:30

SmileYou should be more careful in the future!
System.out.println(subB.getX()+ " " + subB.getY());

4.Re:关于对象在继承关系中的转变的问题。 [Re: caoguanghui] Copy to clipboard
Posted by: jackyangf
Posted on: 2006-03-20 17:42

....
这样编译也不能通过啊?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923