冠县品牌网站建设推广,cdn中国设计网,怎么做app平台,南宁企业网站建设制作我在实践中阅读Java并发性,下面的例子来自于此.我的问题是这个参考逃脱是什么意思#xff1f;会有什么问题#xff1f; .这个引用是如何从doSomething(e)中逃脱的.public class ThisEscape {public ThisEscape(EventSource source) {source.registerListener(new EventListen…我在实践中阅读Java并发性,下面的例子来自于此.我的问题是这个参考逃脱是什么意思会有什么问题 .这个引用是如何从doSomething(e)中逃脱的.public class ThisEscape {public ThisEscape(EventSource source) {source.registerListener(new EventListener() {public void onEvent(Event e) {doSomething(e);}});}}这是如何解决这个问题的public class SafeListener {private final EventListener listener;private SafeListener() {listener new EventListener() {public void onEvent(Event e) {doSomething(e);}};}public static SafeListener newInstance(EventSource source) {SafeListener safe new SafeListener();source.registerListener(safe.listener);return safe;}}编辑我试过以下的例子public class Escape {public Escape( Printer printer ){printer.print(new Escaper(){Overridepublic void parentData(){theCulprit1(Escape.this);}public String name shal;Overridepublic void theCulprit(){System.out.println( this.name );System.out.println( Escape.this.age );}});canAccess();}public void canAccess(){this.age 25;}public String age 62;SuppressWarnings(unused)public static void main(String args[]){Escape escape new Escape(new Printer());}}class Printer{public void print(Escaper escaper){escaper.theCulprit();escaper.parentData();}}class Escaper{public void parentData(){}public void theCulprit(){}public void theCulprit1(Escape escape){System.out.println(escape.age);}}由于逃生物体的构造不完整这输出shal6262我在哪里改变了我的代码public class Escape {private final Escaper escaper;private Escape( ){escaper new Escaper(){Overridepublic void parentData(){theCulprit1(Escape.this);}public String name shal;public void theCulprit(){System.out.println( name );System.out.println( age );}};canAccess();}public void canAccess(){age 25;}public String age 62;public static Escape newInstance( Printer printer){Escape escape new Escape();printer.print(escape.escaper);return escape;}SuppressWarnings(unused)public static void main(String args[]){Escape.newInstance(new Printer());}}在这里.它输出shal2525我对吗 还有任何重新排序的操作,因为在第一个例子中,年龄被初始化为62.即使没有在我的第二个例子中进行自由竞技场决赛,它仍然有效