简体   繁体   English

在java单例中获取内部类的实例

[英]get instance of inner class in java singleton

I have the following arrangement (not showing all fields and methods): 我有以下安排(没有显示所有字段和方法):

public class MyClass implements myClassInterface{

    public static MyClass getInstance(){
       return INSTANCE;
    }

    public class Inner implements innerStuff{
        @Override
         public void doInnerWork(Object param){
         }
    }
}

I need to access doInnerWork(param) . 我需要访问doInnerWork(param) How do I do that? 我怎么做? I try many things but they all fail including: 我尝试了很多东西,但它们都失败了,包括:

new (MyClass.getInstance().Inner()).doInnerWork(param);

thanks for helping. 谢谢你的帮助。

MyClass.getInstance().new Inner().doInnerWork(param);
请注意,这将每次创建一个新的内部类(它不会是单例)。

To create an instance of a nested class Inner , where you want it to be nested inside some object obj , the syntax is 要创建嵌套类Inner的实例,您希望它嵌套在某个对象obj ,语法是

obj.new Inner()

So in your case you probably want 所以在你的情况下你可能想要

MyClass.getInstance().new Inner().doInnerWork(param);

I'm not saying this will give you the results you want, but that's the syntax. 我不是说这会给你想要的结果,但这就是语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM