码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现克隆一个对象

时间:2016-02-02 14:22:16      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

package com.wms;

public class HumanClone {

    public static void main(String[] args) {

        Human hm1 = new Human("张三",60);
        try {
            Human hm2 = (Human) hm1.clone();
            System.out.println(hm1);
            System.out.println(hm2);
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }

}
package com.wms;

public class Human implements Cloneable{

    private String name;
    private int age;
    public Human() {}
    public Human(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Human [name=" + name + ", age=" + age + "]";
    }
    
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    
}

 

Java实现克隆一个对象

标签:

原文地址:http://www.cnblogs.com/sweetstar86/p/5176937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!