astrocn 2005年03月24日
CoreJava的例子,居然严重错误
import java.util.*;

public class CloneList
{

public static void main(String[] args)

{
Employee original = new Employee("Eon Hou",50000);
original.setHireDay(2000,1,1);
Employee copy = (Employee)original.clone();
copy.raiseSalary(10);
copy.setHireDay(2002,1,1);
System.out.println("original=" + original);
System.out.println("copy=" + copy);

}


}

class Employee implements Cloneable
{
public Employee(String n,double s)
{
name = n;
salary=s;

}
public Object clone()
{
try
{
Employee cloned = (Employee)super.clone();
cloned.hireDay = (Date)hireDay.clone();

return hireDay;
}
catch (CloneNotSupportedException e){return null;}

}

public void setHireDay(int year,int month,int day)
{
hireDay = new GregorianCalendar(year,month-1,day).getTime();

}

public void raiseSalary(double byPresent)
{
double raise =salary*byPresent /100;
salary +=raise;
}

public String toString ()
{
return "Employee[name=" + name + ",salary=" + salary + ",gireday ="
+ hireDay + "]";

}

private String name;
private double salary;
private Date hireDay;


}


运行提示,main()严重错误。。。我晕倒
...全文
87 点赞 收藏 6
写回复
6 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告