1、浅拷贝和深拷贝
浅拷贝
浅拷贝只是复制了内存地址,两个引用同时指向一个内存地址,修改其中的一个值,另一个的值也会随之改变。
深拷贝
深拷贝是复制了对象,当一个对象修改了值,另一个不会受影响。
2、java序列化
java提供了一种序列化机制,implements Serializable接口即可。该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据,该对象的类型信息,该对象包含数据的类型信息。将序列化的对象从文件中读取出来可以对其反序列化,也就是说通过对象包含的数据,对象的类型,对象包含的数据类型可以在内存中构建对象。用transient关键字修饰的属性,表明该属性是短暂的不会被序列化传输。
import java.io.*;
/**
* Created by Administrator on 2020/4/12.
*/
public class testSerializable {
public static void main(String[] args){
Employee employee=new Employee();
employee.name="testName";
employee.address="testAddress";
employee.SSN=123;
employee.number=1;
//用ObjectOutputStream 将对象写到文件
try {
FileOutputStream fileOutputStream=new FileOutputStream("serial");
ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(employee);
objectOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//用ObjectInputStream将文件内容读取到对象中
Employee e=new Employee();
try {
FileInputStream fileInputStream=new FileInputStream("serial");
ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
try {
e=(Employee) objectInputStream.readObject();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
fileInputStream.close();
objectInputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(e.name);
System.out.println(e.address);
System.out.println(e.SSN);
System.out.println(e.number);
}
}
//实现Serializable接口
class Employee implements Serializable{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck(){
System.out.println("mailCheck() "+name+" "+address);
}
}
3、对象克隆
-
为什么需要对象克隆
想要对当前对象操作,又想保留原来的数据,这时候就需要对象克隆。 -
如何实现对象克隆
implements Cloneable接口并重写Object类的clone方法
如果引用里又包含引用,就会比较麻烦
/**
* Created by Administrator on 2020/4/12.
*/
public class testClone {
public static void main(String[] args){
Student s1=new Student();
s1.name="student1";
s1.id=1;
s1.professor=new Professor("professor1",45);
Student s2= null;
try {
s2 = (Student) s1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
s2.name="student2";
s2.id=2;
s2.professor.name="professor2";
s2.professor.age=50;
System.out.println(s1);
System.out.println(s2);
}
}
class Student implements Cloneable{
String name;
int id;
Professor professor;
public Object clone() throws CloneNotSupportedException {
Student ret=(Student) super.clone();
ret.professor=(Professor)professor.clone();
return ret;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", professor=" + professor +
'}';
}
}
class Professor implements Cloneable{
String name;
int age;
Professor(String name,int age){
this.name=name;
this.age=age;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Professor{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
implements Serialable接口,通过对象的序列化和反序列化实现克隆,可实现真正的深度克隆
将对象写入字节流再读出来即可实现深拷贝
import java.io.*;
/**
* Created by Administrator on 2020/4/12.
*/
public class testDeepClone {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person p1=new Person();
p1.name="name1";
p1.age=10;
p1.school=new School("school1","Beijing");
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(p1);
ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
Person p2=(Person) ois.readObject();
bos.close();
oos.close();
bis.close();
ois.close();
p2.name="person2";
p2.age=20;
p2.school.name="school2";
p2.school.city="Shanghai";
System.out.println(p1);
System.out.println(p2);
}
}
class Person implements Serializable{
String name;
int age;
School school;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
class School implements Serializable{
String name;
String city;
School(String name,String city){
this.name=name;
this.city=city;
}
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
}