?? clonetest.java.bak
字號:
import java.util.*;
public class CloneTest{
public static void main(String[] args)
{ try
{//某雇員在2000年1.1被雇傭,在同年年底漲工資,調用copy函數在原有數據的基礎上
//只需修改日期,工資這兩個數據其余不變
Employee original=new Employee("John Q.public",5000);
original.setHireDay(2000,1,1);
Employee copy=original.clone();
copy.raiseSalary(10);
copy.setHireDay(2000,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;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -