星空网 > 软件开发 > Java

Java实现克隆一个对象

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实现克隆一个对象

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Metaglossary:https://www.goluckyvip.com/tag/22944.html
MetaKeyword:https://www.goluckyvip.com/tag/22945.html
Metaverse Z:https://www.goluckyvip.com/tag/22946.html
MeUndies:https://www.goluckyvip.com/tag/22949.html
港口码头:https://www.goluckyvip.com/tag/2295.html
MEX摩邑诚:https://www.goluckyvip.com/tag/22950.html
皇帝的皇宫=:https://www.vstour.cn/a/363188.html
海南岛琼海市旅游景点 琼海市的旅游景点:https://www.vstour.cn/a/363189.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流