gpt4 book ai didi

java - 通过公共(public)外部类访问私有(private)内部类 - 来自另一个类

转载 作者:行者123 更新时间:2023-11-29 06:35:56 24 4
gpt4 key购买 nike

我完全坚持这一点,并且一直在研究它一段时间。我有一个类“launcher”,我想通过外部类“PropertyManager”从中访问私有(private)内部类“PropertyInstance”的实例。

所以,在我的启动器中我想写:

        PropertyManager pm = new PropertyManager();
PropertyInstance pi = pm.getInstance("brickbreaker.properties");

在我的 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;
}

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

private class PropertyInstance
{
}

这与 private 修饰符无关。

问题是我无法从我的 Launcher 访问 PropertyInstance 类,因为它是私有(private)的,而且我似乎无法找到解决方法,因此非常感谢任何帮助。如果能做到的话。

编辑:似乎不清楚我正在寻找一种解决方法,同时保持内部类私有(private),对于造成的困惑,我们深表歉意! :)

最佳答案

创建一个接口(interface),定义您想要对私有(private)类的公共(public)访问权限,让您的私有(private)类实现它,并按原样返回。

关于java - 通过公共(public)外部类访问私有(private)内部类 - 来自另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20724136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com