clone问题import java.util.*;public class Clonetest{\x05public static void main(String[] srgs)\x05{\x05\x05try\x05\x05{\x05\x05\x05employee original=new employee("jony",50000);\x05\x05\x05original.sethireday(2000,1,1);\x05\x05\x05employee copy=origi
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 09:56:34
clone问题import java.util.*;public class Clonetest{\x05public static void main(String[] srgs)\x05{\x05\x05try\x05\x05{\x05\x05\x05employee original=new employee("jony",50000);\x05\x05\x05original.sethireday(2000,1,1);\x05\x05\x05employee copy=origi
clone问题
import java.util.*;
public class Clonetest
{
\x05public static void main(String[] srgs)
\x05{
\x05\x05try
\x05\x05{
\x05\x05\x05employee original=new employee("jony",50000);
\x05\x05\x05original.sethireday(2000,1,1);
\x05\x05\x05employee copy=original.clone();
\x05\x05\x05copy.setsalary(10);
\x05\x05\x05copy.sethireday(2002,12,31);
\x05\x05\x05System.out.println("original:"+original);
\x05\x05\x05System.out.println("copy:"+copy);
\x05\x05}
\x05\x05catch(CloneNotSupportedException e)
\x05\x05{
\x05\x05\x05e.printStackTrace();
\x05\x05}
\x05}
\x05
}
class employee implements Cloneable
{
\x05public employee(String n,double s)
\x05{
\x05\x05name=n;
\x05\x05salary=s;
\x05\x05hireday=new Date();
\x05}
\x05
\x05public employee clone() throws CloneNotSupportedException
\x05{
\x05\x05employee cloned=(employee)super.clone();
\x05\x05cloned.hireday=(Date)hireday.clone();
\x05\x05return cloned;
\x05}
\x05
\x05public void sethireday(int year,int month,int day)
\x05{
\x05\x05Date newd=new GregorianCalendar(year,month-1,day).getTime();
\x05\x05hireday.setTime(newd.getTime());
\x05}
\x05public void setsalary(double p){salary+=salary*p/100;}
\x05
\x05public String toSting(){return "employee[name:"+name+",salary:"+salary+",hireday:"+hireday+"]";}
\x05
\x05private Date hireday;
\x05private double salary;
\x05private String name;
}
这是书上的,
System.out.println("original:"+original);
System.out.println("copy:"+copy);
是想输出什么?toString()为什么没有用
可是这里面有重写toString()方法啊
,public String toSting(){return "employee[name:"+name+",salary:"+salary+",hireday:"+hireday+"]";}
为什么输出的是
original:p215.employee@14318bb
copy:p215.employee@ca0b6
clone问题import java.util.*;public class Clonetest{\x05public static void main(String[] srgs)\x05{\x05\x05try\x05\x05{\x05\x05\x05employee original=new employee("jony",50000);\x05\x05\x05original.sethireday(2000,1,1);\x05\x05\x05employee copy=origi
童鞋,你的toString方法写错了...(toSting)
这是常犯的错,重写(override)需要方法签名完全一样,不然就是个新method而已.
加上注解(annotate)@Override可以帮助你
@Override
public String toSting() ...
====
method does not override or implement a method from a supertype
@Override
^
1 error