Skip to content

Commit a67d7da

Browse files
committed
prototype
1 parent 0eba95b commit a67d7da

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# Created by .ignore support plugin (hsz.mobi)
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.classpath
3+
.project
4+
JavaDesignPattern.iml
5+
.idea
6+
.settings
Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
11
package com.anxpp.designpattern.prototype;
2+
23
import java.io.File;
34
import java.io.FileInputStream;
45
import java.io.FileOutputStream;
56
import java.io.ObjectInputStream;
67
import java.io.ObjectOutputStream;
78
import java.io.Serializable;
9+
810
//使用Serializable支持克隆
9-
public class SerializablePrototype implements Serializable{
10-
private static final long serialVersionUID = 1L;
11-
private int i;
12-
private transient int notClone;//transient关键字的成员不会被序列化
13-
public int getI() {
14-
return i;
15-
}
16-
public void setI(int i) {
17-
this.i = i;
18-
}
19-
public int getNotClone() {
20-
return notClone;
21-
}
22-
public void setNotClone(int notClone) {
23-
this.notClone = notClone;
24-
}
25-
public static long getSerialversionuid() {
26-
return serialVersionUID;
27-
}
28-
public void writeToFile(String path) throws Exception{
29-
FileOutputStream outStream = new FileOutputStream(path);
30-
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);
31-
objectOutputStream.writeObject(this);
32-
outStream.close();
33-
}
34-
public SerializablePrototype ReadFromFile(String path) throws Exception{
35-
File file = new File(path);
36-
if(!file.exists())
37-
file.createNewFile();
38-
FileInputStream inStream = new FileInputStream(path);
39-
ObjectInputStream objectOutputStream = new ObjectInputStream(inStream);
40-
Object o= objectOutputStream.readObject();
41-
inStream.close();
42-
return (SerializablePrototype) o;
43-
}
44-
public static void main(String args[]) throws Exception{
45-
String path = "D:/SerializablePrototype.instance";
46-
SerializablePrototype prototype = new SerializablePrototype();
47-
prototype.setI(123);
48-
prototype.setNotClone(456);
49-
prototype.writeToFile(path);
50-
SerializablePrototype prototypeClone = new SerializablePrototype();
51-
prototypeClone = prototype.ReadFromFile(path);
52-
System.out.println(prototypeClone.getI() + " " + prototypeClone.getNotClone() + " ");
53-
}
11+
public class SerializablePrototype implements Serializable {
12+
private static final long serialVersionUID = 1L;
13+
private int i;
14+
private transient int notClone;//transient关键字的成员不会被序列化
15+
16+
public int getI() {
17+
return i;
18+
}
19+
20+
public void setI(int i) {
21+
this.i = i;
22+
}
23+
24+
public int getNotClone() {
25+
return notClone;
26+
}
27+
28+
public void setNotClone(int notClone) {
29+
this.notClone = notClone;
30+
}
31+
32+
public static long getSerialversionuid() {
33+
return serialVersionUID;
34+
}
35+
36+
public void writeToFile(String path) throws Exception {
37+
FileOutputStream outStream = new FileOutputStream(path);
38+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);
39+
objectOutputStream.writeObject(this);
40+
outStream.close();
41+
}
42+
43+
public SerializablePrototype ReadFromFile(String path) throws Exception {
44+
File file = new File(path);
45+
if (!file.exists())
46+
file.createNewFile();
47+
FileInputStream inStream = new FileInputStream(path);
48+
ObjectInputStream objectOutputStream = new ObjectInputStream(inStream);
49+
Object o = objectOutputStream.readObject();
50+
inStream.close();
51+
return (SerializablePrototype) o;
52+
}
53+
54+
public static void main(String args[]) throws Exception {
55+
String path = "SerializablePrototype.instance";
56+
SerializablePrototype prototype = new SerializablePrototype();
57+
prototype.setI(123);
58+
prototype.setNotClone(456);
59+
prototype.writeToFile(path);
60+
SerializablePrototype prototypeClone = new SerializablePrototype();
61+
prototypeClone = prototype.ReadFromFile(path);
62+
System.out.println(prototypeClone.getI() + " " + prototypeClone.getNotClone() + " ");
63+
}
5464
}

0 commit comments

Comments
 (0)