58,452
社区成员




import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Client2_3 {
public static void main(String[] args) {
Country america = new Country();
Population p1 = new Population();
p1.setPpl(10000);
america.setP(p1);
int america_ppl = america.getP().getPpl();
Country china = (Country) Country.deepClone(new Country());
china.getP().setPpl(20000);
int china_ppl = china.getP().getPpl();
System.out.println("the population of America is "+america_ppl);
System.out.println("the population of China is "+china_ppl);
}
}
class Population implements Serializable {
private int ppl;
public int getPpl() {
return ppl;
}
public void setPpl(int ppl) {
this.ppl = ppl;
}
}
class Country implements Serializable{
private Population p;
public Population getP() {
return p;
}
public void setP(Population p) {
this.p = p;
}
public static Object deepClone(Object src) {
Object dst = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
dst = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dst;
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Client2_3 {
public static void main(String[] args) {
Country america = new Country();
Population p1 = new Population();
p1.setPpl(10000);
america.setP(p1);
int america_ppl = america.getP().getPpl();
Country china = (Country) Country.deepClone(america);
china.getP().setPpl(20000);
int china_ppl = china.getP().getPpl();
System.out.println("the population of America is " + america_ppl);
System.out.println("the population of China is " + china_ppl);
}
}
class Population implements Serializable {
private int ppl;
public int getPpl() {
return ppl;
}
public void setPpl(int ppl) {
this.ppl = ppl;
}
}
class Country implements Serializable {
private Population p;
public Population getP() {
return p;
}
public void setP(Population p) {
this.p = p;
}
public static Object deepClone(Object src) {
Object dst = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
dst = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dst;
}
}
// 运行结果
// the population of America is 10000
// the population of China is 20000