英文:
How to expose methods of a private class?
问题
我正在尝试编写一个库,让我们称之为myLib
。
public class MyLib {
public SomeClass someClass = SomeClass.INSTANCE();
private static MyLib _instance = null;
public static MyLib INSTANCE() {
if (_instance == null)
_instance = new MyLib();
return _instance;
}
}
SomeClass
看起来像这样:
class SomeClass {
private static SomeClass _instance = null;
public static SomeClass INSTANCE() {
if (_instance == null)
_instance = new SomeClass();
return _instance;
}
public void print(String message) {
System.out.println(message);
}
}
我希望用户能够编写:MyLib.INSTANCE().someClass.print("Hello World!");
然而,在其当前形式中,print
是不可访问的。我不希望用户能够简单地绕过 MyLib.INSTANCE()
并编写 SomeClass.INSTANCE().print("Hello World");
,因为有一些初始化操作。
我该如何解决这个问题?
英文:
I am trying to write a library. Let's call it myLib
.
public class MyLib {
public SomeClass someClass = SomeClass.INSTANCE();
private static MyLib _instance = null;
public static MyLib INSTANCE()
{
if (_instance == null)
_instance = new MyLib();
return _instance;
}
}
SomeClass
looks like this:
class SomeClass {
private static SomeClass _instance = null;
public static SomeClass INSTANCE()
{
if (_instance == null)
_instance = new SomeClass();
return _instance;
}
public void print(String message){
System.out.println(message);
}
}
I'd like users to be able to write: MyLib.INSTANCE().someClass.print("Hello World!");
However, in its current form, print
is inaccessible. I do not want the user to be able to simply bypass MyLib.INSTANCE()
and write SomeClass.INSTANCE().print("Hello World");
because some initialisation is done.
How can I solve this?
答案1
得分: 1
要使 SomeClass.print
可访问,需要使 SomeClass
本身可访问。
要使 SomeClass.INSTANCE
不可访问,请移除 public
修饰符。由于您只从相同的包中访问,可以使用没有显式访问修饰符的 "default" 访问方式:
// public 访问
public class SomeClass {
// 仅包内访问
static SomeClass INSTANCE() { }
// public 访问
public void print(String message) { }
}
英文:
To make SomeClass.print
accessible, SomeClass
itself needs to be accessible.
To make SomeClass.INSTANCE
not accessible, remove the public
modifier. Since you are only accessing from the same package, use the "default" access which has no explicit access modifier:
// public access
public class SomeClass {
// package-only access
static SomeClass INSTANCE() { }
// public access
public void print(String message) { }
}
答案2
得分: 0
你必须将SomeClass
保持为内部私有类,以防止其在MyLib
类中被直接访问和使用。
public static class MyLib {
public SomeClass someClass = SomeClass.INSTANCE();
private static MyLib _instance = null;
public static MyLib INSTANCE() {
if (_instance == null)
_instance = new MyLib();
return _instance;
}
private static class SomeClass {
private static SomeClass _instance = null;
public static SomeClass INSTANCE() {
if (_instance == null)
_instance = new SomeClass();
return _instance;
}
public void print(String message) {
System.out.println(message);
}
}
}
编辑:
在不使用嵌套类的情况下,使用protected
修饰符的解决方案。
com.lib
包中的MyLib
类
package com.lib;
public class MyLib {
public SomeClass someClass = SomeClass.INSTANCE();
private static MyLib _instance = null;
public static MyLib INSTANCE() {
if (_instance == null)
_instance = new MyLib();
return _instance;
}
}
com.lib
包中的SomeClass
类
package com.lib;
public class SomeClass {
private static SomeClass _instance = null;
protected static SomeClass INSTANCE() {
if (_instance == null)
_instance = new SomeClass();
return _instance;
}
public void print(String message) {
System.out.println(message);
}
}
在主项目中访问它(即不同的包)
package com.project;
import com.lib.MyLib;
public class MainClass {
public static void main(String[] args) {
MyLib.INSTANCE().someClass.print("Hello World!");
}
}
英文:
You have to keep SomeClass
as an inner private class to prevent its direct accessibility and usage in MyLib
class.
public static class MyLib {
public SomeClass someClass = SomeClass.INSTANCE();
private static MyLib _instance = null;
public static MyLib INSTANCE()
{
if (_instance == null)
_instance = new MyLib();
return _instance;
}
private static class SomeClass {
private static SomeClass _instance = null;
public static SomeClass INSTANCE()
{
if (_instance == null)
_instance = new SomeClass();
return _instance;
}
public void print(String message){
System.out.println(message);
}
}
}
Edit:
The solution to do without nested class using protected
modifier
MyLib
class in com.lib package
package com.lib;
public class MyLib {
public SomeClass someClass = SomeClass.INSTANCE();
private static MyLib _instance = null;
public static MyLib INSTANCE()
{
if (_instance == null)
_instance = new MyLib();
return _instance;
}
}
SomeClass
class in com.lib package
package com.lib;
public class SomeClass {
private static SomeClass _instance = null;
protected static SomeClass INSTANCE()
{
if (_instance == null)
_instance = new SomeClass();
return _instance;
}
public void print(String message){
System.out.println(message);
}
}
Accessing it in your main project (i.e. different package)
package com.project;
public class MainClass {
public static void main(String[] args) {
MyLib.INSTANCE().someClass.print("Hello World!");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论