从另一个类访问私有内部类,通过公共外部类

Dyl*_*eus 1 java

我完全坚持这一点,并且已经玩了一段时间.我有一个类"启动器",我想从中通过外部类"PropertyManager"访问私有内部类"PropertyInstance"的实例.

所以,在我的发射器中,我想写:

        PropertyManager pm = new PropertyManager();
        PropertyInstance pi = pm.getInstance("brickbreaker.properties");
Run Code Online (Sandbox Code Playgroud)

在我的PropertyManager类中,我编写了以下代码:

public PropertyInstance getInstance(String location)
{
    PropertyInstance pi = null;

    if(!propertyList.contains(location))
    {
        System.out.println("it does not contain it yet, so we will create it");
        pi = new PropertyInstance(location);
        propertyList.add(pi);
    }

    return pi;
}
Run Code Online (Sandbox Code Playgroud)

在这个类中,我有以下内部类:

private class PropertyInstance
{
}
Run Code Online (Sandbox Code Playgroud)

除了私有修饰符之外,这是无关紧要的.

问题是我无法从我的Launcher访问PropertyInstance类,因为它是私有的,我似乎无法找到解决方法,所以真的有任何帮助.如果它甚至可以做到.

编辑:似乎不清楚我正在寻找一个解决方法,同时保持内部阶级私密,抱歉混乱!:)

bma*_*ies 5

创建一个接口,定义您希望私有类的公共访问,让您的私有类实现它,并将其返回.