北京酒仙桥危房最新:程序高手来看看

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 03:39:17
package mytest;
public class test{
public test(){
}
public static void main(String[] argv){
test1 t1=new test1();
test2 t2=new test2();
test1 t3=null;
System.out.println(t1.toString());
t3=t1;
t1=(test1)t2;
System.out.println(t1.toString());
t2=(test2)t3;
System.out.pringln(t2.tostring());
}
}
class test1(){
public String toString(){
return "this is test1!";
}
}
class test2 extends test1{
public String toString(){
return "this is test2!";
}
}

请问结果。
我运行老是出错。

1.System.out.pringln(t2.tostring()); 改为System.out.println(t2.toString());
2.class test1(){ 改为class test1{
3.t2=(test2)t3;这句是用向下类型转换,在Java中这样是要抛出ClassCastException的,因为无法将你的父类test1转到test2上。