[英]How to refer to outer “this” object from an inner class after de-serialization in Java?
我有一個使用內部類序列化的對象。 反序列化后,內部類在外部類上調用一個方法,該方法引用“ this”,這給我帶來了問題。 我的代碼:
// Outer Class
public AuctionItem getInstance(){
return this;
}
private class ItemCallBack extends TimerTask {
@Override
public void run() {
try {
AuctionServerIntf aServer = (AuctionServerIntf) Naming.lookup("rmi://"+AuctionServer.HOST+":"+AuctionServer.PORT+"/AuctionServer");
aServer.notifyItemEnded(getInstance()); // <--- Exception is thrown here
} catch (RemoteException | MalformedURLException | NotBoundException e) {
e.printStackTrace();
}
}
}
如您所見,我想將對外部類的引用傳遞給aServer.notifyItemEnded()方法,在序列化之前它可以正常工作,但之后會引發連接異常。 任何想法如何解決這個問題? 干杯。
嘗試在外部類的類名之前添加:
aServer.notifyItemEnded(AuctionItem.this.getInstance());
嘗試使用為作業提供的Java語法:
aServer.notifyItemEnded(AuctionItem.this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.