gpt4 book ai didi

java - 如何使代码深处的另一个对象可以访问对象?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:02 25 4
gpt4 key购买 nike

在 java 中,我正在将属性文件读入 Properties 对象:

  static Properties fileProp = null;  // holds file properties
public static void main( final String[] args ) throws IOException
{
...
//load a properties file
InputStream is = LearnButtonPressHttpHandler.class.getResourceAsStream("/rsrc/file.properties");

fileProp.load(is);
...

// more objects are created

}

后来,深入代码,深入几层,我需要访问这个properties对象,但发现我没有访问这个fileProp对象的权限。我不想将这个对象作为参数向下传递到我需要它的许多层。它会解决问题,但似乎不是一个优雅的解决方案。有没有更好的办法?

最佳答案

fileProp 引用创建一个 static getter:

public static Properties getFileProp()
{
return fileProp;
}

这样,任何其他需要它的代码都可以访问它。

关于java - 如何使代码深处的另一个对象可以访问对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475975/

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