public class DepthCopy {
public static void main(String[] args) {
Copy first = new Copy("hzw", 24);
Copy second = first;
second.name = "shanxi";
System.out.println(first.name);//输出shanxi
}
}
class Copy
{
public String name;
public int age;
public Copy(String name,int age) {
this.name = name;
this.age = age;
}
}
public class DepthCopy {
public static void main(String[] args) {
Copy first = new Copy("hzw", 24);
Copy second = null;
try {
second = (Copy) first.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
second.name = "shanxi";
System.out.println(first.name);//输出: hzw
System.out.println(first);//输出: com.hzw.day33.Copy@7f39ebdb
System.out.println(second);//输出: com.hzw.day33.Copy@33abb81e
}
}
class Copy implements Cloneable
{
public String name;
public int age;
public Copy(String name,int age) {
this.name = name;
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class DepthCopy {
public static void main(String[] args) {
Student student = new Student(95);
Copy first = new Copy("hzw", 24,student);
Copy second = null;
try {
second = (Copy) first.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
second.name = "shanxi";
second.student.score = 60;
System.out.println(first == second);//false
System.out.println(first.student == second.student);//true
System.out.println(first.student.score);//60
}
}
class Copy implements Cloneable
{
public String name;
public int age;
public Student student;
public Copy(String name,int age,Student student) {
this.name = name;
this.age = age;
this.student = student;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student
{
public int score;
public Student(int score) {
this.score = score;
}
}
public class DepthCopy {
public static void main(String[] args) {
Student student = new Student(95);
Copy first = new Copy("hzw", 24,student);
Copy second = null;
try {
second = (Copy) first.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
second.name = "shanxi";
second.student.score = 60;
System.out.println(first == second);//false
System.out.println(first.student == second.student);//false
System.out.println(first.student.score);//95
System.out.println(second.student.score);//60
}
}
class Copy implements Cloneable
{
public String name;
public int age;
public Student student;
public Copy(String name,int age,Student student) {
this.name = name;
this.age = age;
this.student = student;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Copy copy = (Copy)super.clone();
copy.student = (Student) student.clone();
return copy;
}
}
class Student implements Cloneable
{
public int score;
public Student(int score) {
this.score = score;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class DepthCopy {
public static void main(String[] args) {
College school = new College("nongda");
Student student = new Student(95, school);
Copy copy = new Copy("hzw",23, student);
Copy another = null;//表示反序列化出来的类实例
//进行序列化操作
try {
FileOutputStream fos = new FileOutputStream(new File("d:/copy.txt"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(copy);
} catch (Exception e) {
e.printStackTrace();
}
//进行反序列化操作
FileInputStream fis;
try {
fis = new FileInputStream(new File("d:/copy.txt"));
ObjectInputStream ois = new ObjectInputStream(fis);
another = (Copy) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(copy == another);//false
System.out.println(copy.student == another.student);//false
System.out.println(copy.student.school == another.student.school);//false
another.student.school.schoolName = "wuda";
System.out.println(copy.student.school.schoolName);//nongda
}
}
class Copy implements Serializable
{
public String name;
public int age;
public Student student;
public Copy(String name,int age,Student student) {
this.name = name;
this.age = age;
this.student = student;
}
}
class Student implements Serializable
{
public int score;
public College school;
public Student(int score,College school) {
this.score = score;
this.school = school;
}
}
class College implements Serializable
{
public String schoolName;
public College(String schoolName) {
this.schoolName = schoolName;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有