/*
2008年6月23日16:29:06
为什么 import java.util.*; 不起作用?
*/import java.util.*; //6行import java.util.Date; //8行
import java.util.GregorianCalendar; //9行 为什么8行和9行注释掉就会出错,第6行的代码不是已经把util包中的类全部导入了么? 请指教 对了,我用的编译器是j2sdk1.6public class CloneTest
{
public static void main(String[] args)
{
try
{
Employee original = new Employee("John Q. Public", 50000);
original.setHireDay(2000, 1, 1);
Employee copy = original.clone();
copy.raiseSalary(10);
copy.setHireDay(2002, 12, 31);
System.out.println("original=" + original);
System.out.println("copy=" + copy);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}class Employee implements Cloneable
{
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee clone() throws CloneNotSupportedException
{
// call Object.clone()
Employee cloned = (Employee)super.clone();
// clone mutable fields
cloned.hireDay = (Date)hireDay.clone();
return cloned;
} /**
Set the hire day to a given date
@param year the year of the hire day
@param month the month of the hire day
@param day the day of the hire day
*/
public void setHireDay(int year, int month, int day)
{
hireDay = new GregorianCalendar(year, month - 1, day).getTime();
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} public String toString()
{
return "Employee[name=" + name
+ ",salary=" + salary
+ ",hireDay=" + hireDay
+ "]";
} private String name;
private double salary;
private Date hireDay;
}
2008年6月23日16:29:06
为什么 import java.util.*; 不起作用?
*/import java.util.*; //6行import java.util.Date; //8行
import java.util.GregorianCalendar; //9行 为什么8行和9行注释掉就会出错,第6行的代码不是已经把util包中的类全部导入了么? 请指教 对了,我用的编译器是j2sdk1.6public class CloneTest
{
public static void main(String[] args)
{
try
{
Employee original = new Employee("John Q. Public", 50000);
original.setHireDay(2000, 1, 1);
Employee copy = original.clone();
copy.raiseSalary(10);
copy.setHireDay(2002, 12, 31);
System.out.println("original=" + original);
System.out.println("copy=" + copy);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}class Employee implements Cloneable
{
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee clone() throws CloneNotSupportedException
{
// call Object.clone()
Employee cloned = (Employee)super.clone();
// clone mutable fields
cloned.hireDay = (Date)hireDay.clone();
return cloned;
} /**
Set the hire day to a given date
@param year the year of the hire day
@param month the month of the hire day
@param day the day of the hire day
*/
public void setHireDay(int year, int month, int day)
{
hireDay = new GregorianCalendar(year, month - 1, day).getTime();
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} public String toString()
{
return "Employee[name=" + name
+ ",salary=" + salary
+ ",hireDay=" + hireDay
+ "]";
} private String name;
private double salary;
private Date hireDay;
}
楼主【xjyr】截止到2008-06-24 11:33:48的历史汇总数据(不包括此帖):
发帖数:56 发帖分:1120
结贴数:8 结贴分:160
未结数:48 未结分:960
结贴率:14.29 % 结分率:14.29 %
public Employee clone() throws CloneNotSupportedException
{
// call Object.clone()
Employee cloned = (Employee)super.clone();
// clone mutable fields
cloned.hireDay = (Date)hireDay.clone();
return cloned;
}
这个地方,返回值应该是Object
就是你要改成public Object clone() throws CloneNotSupportedException
import java.util.GregorianCalendar;//9
既然已经导入了import java.util.*;
那么8行和9行是不是多此一举吗?重复导入类。