clone问题import java.util.*;public class Clonetest{\x05public

clone问题
import java.util.*;
public class Clonetest
{
x05public static void main(String[] srgs)
x05{
x05x05try
x05x05{
x05x05x05employee original=new employee("jony",50000);
x05x05x05original.sethireday(2000,1,1);
x05x05x05employee copy=original.clone();
x05x05x05copy.setsalary(10);
x05x05x05copy.sethireday(2002,12,31);
x05x05x05System.out.println("original:"+original);
x05x05x05System.out.println("copy:"+copy);
x05x05}
x05x05catch(CloneNotSupportedException e)
x05x05{
x05x05x05e.printStackTrace();
x05x05}
x05}
x05
}
class employee implements Cloneable
{
x05public employee(String n,double s)
x05{
x05x05name=n;
x05x05salary=s;
x05x05hireday=new Date();
x05}
x05
x05public employee clone() throws CloneNotSupportedException
x05{
x05x05employee cloned=(employee)super.clone();
x05x05cloned.hireday=(Date)hireday.clone();
x05x05return cloned;
x05}
x05
x05public void sethireday(int year,int month,int day)
x05{
x05x05Date newd=new GregorianCalendar(year,month-1,day).getTime();
x05x05hireday.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
zhuningwll 1年前 已收到1个回答 举报

e1682201 花朵

共回答了27个问题采纳率:85.2% 举报

童鞋,你的toString方法写错了...(toSting)
这是常犯的错,重写(override)需要方法签名完全一样,不然就是个新method而已.
加上注解(annotate)@Override可以帮助你
@Override
public String toSting() ...
====
method does not override or implement a method from a supertype
@Override
^
1 erro

1年前 追问

8

zhuningwll 举报

不好意思 我是新手 你写的这些完全看不懂 而且我这是JAVA核心技术书上的例子,我没有改动,只是觉得结果有问题,书上也没有写结果

举报 e1682201

好吧 你仔细看:你要的是toString方法,但你的代码里写的是toSting,你写错了或者书上印错了。 你把toSting 改成toString就起作用了。
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.020 s. - webmaster@yulucn.com