时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 创建深拷贝
Java 创建深拷贝
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
365
查看 / 运行 实例源码
创建深拷贝
实例源码:
源代码:
执行
执行中...
public class CloneTest2 { public static void main(String[] args) {//from N o w J a v a . c o m Employee emp1 = new Employee("M", "A"); emp1.setSalary(40000.0); emp1.address = new Address("1300 N. Main Street","New York", "CA", "99999"); Employee emp2 = (Employee)emp1.clone(); System.out.println("**** after cloning ****\n"); printEmployee(emp1); printEmployee(emp2); emp2.setLastName("S"); emp2.address = new Address("2503 N. 6th Street","L.A.", "CA", "93722"); /*来自 时代Java公众号*/ System.out.println( "**** after changing emp2 ****\n"); printEmployee(emp1); printEmployee(emp2); } private static void printEmployee(Employee e) { System.out.println(e.getFirstName() + " " + e.getLastName()); System.out.println(e.address.getAddress()); System.out.println("Salary: " + e.getSalary()); System.out.println(); } } class Employee implements Cloneable { private String lastName; private String firstName; private Double salary; public Address address; public Employee(String lastName, String firstName) { this.lastName = lastName; this.firstName = firstName; this.address = new Address(); } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public Double getSalary() { return this.salary; } public void setSalary(Double salary) { this.salary = salary; } public Object clone() { Employee emp; try { emp = (Employee) super.clone(); emp.address = (Address)address.clone(); } catch (CloneNotSupportedException e) { return null; // will never happen } return emp; } public String toString() { return this.getClass().getName() + "[" + this.firstName + " " + this.lastName + ", " + this.salary + "]"; } } class Address implements Cloneable { private String street; private String city; private String state; private String zipCode; public Address() { this.street = ""; this.city = ""; this.state = ""; this.zipCode = ""; } public Address(String street, String city, String state, String zipCode) /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
**** after cloning ****
A M
1300 N. Main Street
New York, CA 99999
Salary: 40000.0
A M
1300 N. Main Street
New York, CA 99999
Salary: 40000.0
**** after changing emp2 ****
A M
1300 N. Main Street
New York, CA 99999
Salary: 40000.0
A S
2503 N. 6th Street
L.A., CA 93722
Salary: 40000.0
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部