如何暴露私有类的方法?

huangapple go评论75阅读模式
英文:

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!");
    }
}

huangapple
  • 本文由 发表于 2020年8月13日 22:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定