在学习《大话设计模式》原型模型的时候,总感觉就是在教浅拷贝和深拷贝的区别与实现方法。
在我的理解看来,原型模式就是利用了浅拷贝、深拷贝的知识。用python和java两种语言实现浅拷贝,深拷贝。
而且在深入了解中,对python的拷贝有了更深的了解。
浅拷贝和深拷贝-Python
用copy()方法
from copy import copy
class GirlFriend:
name = "兰霸虎"
class Person:
name = "张三"
girlfriend = GirlFriend()
def getInformation(self):
print("name = "+self.name)
print("girlfriend = "+ self.girlfriend.name)
print()
person = Person()
person.getInformation()
#浅拷贝
person2 = copy(person)
person2.name = "李四"
person2.girlfriend.name = "蔡*KUN"
person.getInformation()
person2.getInformation()
'''
结果
name = 张三
girlfriend = 兰霸虎
name = 张三
girlfriend = 蔡*KUN
name = 李四
girlfriend = 蔡*KUN
'''
很可惜的是,我本以为deepcopy()能实现我想要的深拷贝,然鹅在此处deepcopy()和copy()效果是一样的
from copy import deepcopy
class GirlFriend:
name = "兰霸虎"
class Person:
name = "张三"
girlfriend = GirlFriend()
def getInformation(self):
print("name = "+self.name)
print("girlfriend = "+ self.girlfriend.name)
print()
person = Person()
person.getInformation()
#深拷贝
person2 = deepcopy(person)
person2.name = "李四"
person2.girlfriend.name = "蔡*KUN"
person.getInformation()
person2.getInformation()
'''
结果
name = 张三
girlfriend = 兰霸虎
name = 张三
girlfriend = 蔡*KUN
name = 李四
girlfriend = 蔡*KUN
'''
再试试序列化和反序列化。
#!/usr/bin/python
import pickle
class GirlFriend:
name = "兰霸虎"
class Person:
name = "张三"
girlfriend = GirlFriend()
def getInformation(self):
print("name = "+self.name)
print("girlfriend = "+ self.girlfriend.name)
print()
person = Person()
person.getInformation()
pickle.dump(person,open('a.txt','wb'))
person1 = pickle.load(open('a.txt','rb'))
person1.name = "李四"
person1.girlfriend.name = "蔡*KUN"
person.getInformation()
person1.getInformation()
'''
结果
name = 张三
girlfriend = 兰霸虎
name = 张三
girlfriend = 蔡*KUN
name = 李四
girlfriend = 蔡*KUN
'''
败,可能是我想法出了错,方法用错了,实际上也许可以实现。希望有老哥看到了能告诉小弟一下。
想改变李四的girlfriend而不影响李三的,就只能直接赋予一个新的girlfriend对象。copy()和deepcopy()的结果都一样
#!/usr/bin/python
from copy import deepcopy,copy
class GirlFriend:
name = "兰霸虎"
class Person:
name = "张三"
girlfriend = GirlFriend()
def getInformation(self):
print("name = "+self.name)
print("girlfriend = "+ self.girlfriend.name)
print()
person = Person()
person.getInformation()
girlfriend1 = GirlFriend()
girlfriend1.name = "C"
person.girlfriend= girlfriend1
person2 = copy(person)
person2.name = "李四"
girlfriend2 = GirlFriend()
girlfriend2.name = "B"
person2.girlfriend = girlfriend2
person.getInformation()
person2.getInformation()
'''
结果
name = 张三
girlfriend = 兰霸虎
name = 张三
girlfriend = C
name = 李四
girlfriend = B
'''
浅拷贝与深拷贝-Java
浅拷贝
public class Example{
public static void main (String[] args){
Person person = new Person("张三");
Person person1 = person.clone();
person1.name = "李四";
person.print();
person1.print();
person1.girlfriend.name = "蔡*KUN";
person.print();
person1.print();
}
}
class GirlFriend{
public String name = "兰霸虎" ;
}
class Person implements Cloneable{
public String name;
public GirlFriend girlfriend = new GirlFriend();
Person(String str){
name = str;
}
public void print(){
System.out.println("name = " + name);
System.out.println("girlfriend = " + girlfriend.name);
System.out.println();
}
public Person clone(){
Person person_clone = null;
try{
person_clone = (Person)super.clone(); //若不向下转换,return的是一个Object类
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return person_clone;
}
}
/*
结果
name = 张三
girlfriend = 兰霸虎
name = 李四
girlfriend = 兰霸虎
name = 张三
girlfriend = 蔡*KUN
name = 李四
girlfriend = 蔡*KUN
*/
深拷贝
public class Example{
public static void main (String[] args){
Person person = new Person("张三");
Person person1 = person.clone();
person1.name = "李四";
person.print();
person1.print();
person1.girlfriend.name = "蔡*KUN";
person.print();
person1.print();
}
}
class GirlFriend implements Cloneable{
public String name = "兰霸虎" ;
public GirlFriend clone(){
GirlFriend girlfriend_clone = null;
try{
girlfriend_clone = (GirlFriend)super.clone();
}catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return girlfriend_clone;
}
}
class Person implements Cloneable{
public String name;
public GirlFriend girlfriend = new GirlFriend();
Person(String str){
name = str;
}
public void print(){
System.out.println("name = " + name);
System.out.println("girlfriend = " + girlfriend.name);
System.out.println();
}
public Person clone(){
Person person_clone = null;
try{
person_clone = (Person)super.clone(); //若不向下转换,return的是一个Object类
person_clone.girlfriend = (GirlFriend)girlfriend.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return person_clone;
}
}
/*
结果
name = 张三
girlfriend = 兰霸虎
name = 李四
girlfriend = 兰霸虎
name = 张三
girlfriend = 兰霸虎
name = 李四
girlfriend = 蔡*KUN
*/
再试试序列化和反序列化
import java.io.*;
public class Example{
public static void main (String[] args){
Person person = new Person("张三");
Person person1 = null;
try{
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(person);
byte[] bytes = os.toByteArray();
oos.close();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
person1 = (Person) ois.readObject() ;
ois.close();
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
person1.name = "李四";
person.print();
person1.print();
person1.girlfriend.name = "蔡*KUN";
person.print();
person1.print();
}
}
class GirlFriend implements Serializable{
public static final long serialVersionUID = -654654612312356489L;
public String name = "兰霸虎" ;
}
class Person implements Serializable{
public static final long serialVersionUID = -65465465465456489L;
public String name;
public GirlFriend girlfriend = new GirlFriend();
Person(String str){
name = str;
}
public void print(){
System.out.println("name = " + name);
System.out.println("girlfriend = " + girlfriend.name);
System.out.println();
}
}
/*
结果
name = 张三
girlfriend = 兰霸虎
name = 李四
girlfriend = 兰霸虎
name = 张三
girlfriend = 兰霸虎
name = 李四
girlfriend = 蔡*KUN
*/
就当给自己复习了一遍拷贝有关的知识