NoSuchMethodError 错误

mxk0921 mxk0921 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
用序列化方法写深克隆时出现这个错误。求高手解答

Exception in thread "main" java.lang.NoSuchMethodError: Country.setP(LPopulation;)V
at Client2_3.main(Client2_3.java:14)



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;
}
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
mxk0921
Eclipse 58,439 成员 49,457 内容