java 克隆

浅复制

import java.util.Date;
import java.util.GregorianCalendar;

class Employee implements Cloneable{
    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String n, double s){
        name = n;
        salary = s;
        hireDay = new Date();
    }

    public Employee clone() throws CloneNotSupportedException{
        Employee cloned = (Employee)super.clone();

       // cloned.hireDay = (Date)hireDay.clone();

        return cloned;
    }

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

        hireDay.setTime(newHireDay.getTime());
    }

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

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

}


public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
        try {
            Employee original = new Employee("tom",5000);
            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();
        }

    }
}

运行结果:

original=Employee[name=tom,salary=5000.0, hireDay=Tue Dec 31 00:00:00 CST 2002]

copy=Employee[name=tom,salary=5500.0, hireDay=Tue Dec 31 00:00:00 CST 2002]

深复制:

import java.util.Date;
import java.util.GregorianCalendar;

class Employee implements Cloneable{
    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String n, double s){
        name = n;
        salary = s;
        hireDay = new Date();
    }

    public Employee clone() throws CloneNotSupportedException{
        Employee cloned = (Employee)super.clone();

        cloned.hireDay = (Date)hireDay.clone();

        return cloned;
    }

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

        hireDay.setTime(newHireDay.getTime());
    }

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

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

}


public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
        try {
            Employee original = new Employee("tom",5000);
            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();
        }

    }
}

运行结果:

original=Employee[name=tom,salary=5000.0, hireDay=Sat Jan 01 00:00:00 CST 2000]
copy=Employee[name=tom,salary=5500.0, hireDay=Tue Dec 31 00:00:00 CST 2002]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值