[求助]请教关于对象克隆的问题!!
请问一下,我的程序为什么会编译不了??但如果把第9行删除,并把第61行的注释取消,则程序就可以运行!!
class lian
{
public static void main(String []args)
{
teacher z=new teacher("李老师",50);
student x=new student("pet",20,z);
student y=x.clone();
y.name="petpet";
teacher y.z=z.clone(); //第9行
y.z.name="Mr Li";
System.out.println(y.name);
System.out.println(z.name);
}
}
class teacher implements Cloneable
{
int age;
String name;
teacher(String name,int age)
{
this.name=name;
this.age=age;
}
public teacher clone()
{
teacher q=null;
try
{
q=(teacher)super.clone();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
return q;
}
}
class student implements Cloneable
{
int age;
String name;
student o;
teacher z;
student(String name,int age,teacher z)
{
this.name=name;
this.age=age;
this.z=z;
}
public student clone()
{
student o=null;
try
{
o=(student)super.clone();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
//o.z=(teacher)z.clone(); //第61行
return o;
}
}
----------------解决方案--------------------------------------------------------
以下是引用向着梦想加速在2007-9-27 23:32:37的发言:
请问一下,我的程序为什么会编译不了??
但如果把第9行删除,并把第61行的注释取消,则程序就可以运行!!
class lian
{
public static void main(String []args)
{
teacher z=new teacher("李老师",50);
student x=new student("pet",20,z);
student y=x.clone();
y.name="petpet";
y.z=z.clone(); //第9行把teacher去掉,不过建议类名首字母大写
y.z.name="Mr Li";
System.out.println(y.name);
System.out.println(z.name);
}
}
class teacher implements Cloneable
{
int age;
String name;
teacher(String name,int age)
{
this.name=name;
this.age=age;
}
public teacher clone()
{
teacher q=null;
try
{
q=(teacher)super.clone();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
return q;
}
}
class student implements Cloneable
{
int age;
String name;
student o;
teacher z;
student(String name,int age,teacher z)
{
this.name=name;
this.age=age;
this.z=z;
}
public student clone()
{
student o=null;
try
{
o=(student)super.clone();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
//o.z=(teacher)z.clone(); //第61行
return o;
}
}
请问一下,我的程序为什么会编译不了??
但如果把第9行删除,并把第61行的注释取消,则程序就可以运行!!
class lian
{
public static void main(String []args)
{
teacher z=new teacher("李老师",50);
student x=new student("pet",20,z);
student y=x.clone();
y.name="petpet";
y.z=z.clone(); //第9行把teacher去掉,不过建议类名首字母大写
y.z.name="Mr Li";
System.out.println(y.name);
System.out.println(z.name);
}
}
class teacher implements Cloneable
{
int age;
String name;
teacher(String name,int age)
{
this.name=name;
this.age=age;
}
public teacher clone()
{
teacher q=null;
try
{
q=(teacher)super.clone();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
return q;
}
}
class student implements Cloneable
{
int age;
String name;
student o;
teacher z;
student(String name,int age,teacher z)
{
this.name=name;
this.age=age;
this.z=z;
}
public student clone()
{
student o=null;
try
{
o=(student)super.clone();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
//o.z=(teacher)z.clone(); //第61行
return o;
}
}
----------------解决方案--------------------------------------------------------