Java:如何限制对特定类的方法访问?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11549394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to limit access of a method to a specific class?
提问by IDDQD
Here's an example:
下面是一个例子:
class A
{
List l = new List ();
list.insert("x");
}
class List
{
...
public void insert ()
{
/*insertion occurs*/
}
...
}
Is it possible at all to keep the insert() method public, but limit access only to class A so that no other class can access it, only when called from A?
是否有可能将 insert() 方法保持公开,但只限制对类 A 的访问,以便其他类不能访问它,只有在从 A 调用时才能访问它?
回答by 1r0n1k
I would pass the object that is calling the method as an argument, i.e.
我会将调用该方法的对象作为参数传递,即
list.insert("x", this);
And then check if the passed Object is an Instance of Class A
然后检查传入的Object是否是Class A的实例
public void insert (String x, Object o)
{
if(o instanceof ClassA){
/*insertion occurs*/
}
}
回答by dasblinkenlight
If the method is public, everyone can access it. The trick to access control like yours is to expose a set of public operations through an interface, add auxiliary operations to a private class implementing the interface, and make your users program to the interface, not to a class.
如果方法是公开的,那么每个人都可以访问它。像您这样访问控制的技巧是通过接口公开一组公共操作,向实现接口的私有类添加辅助操作,并使您的用户编程到接口,而不是类。
Here is an example:
下面是一个例子:
public interface MyList {
Object elementAt(int i);
}
public class A {
private static class MyListImpl implements MyList {
public Object elementAt(int i) {
...
}
public void insert(Object element) {
...
}
}
private final MyListImpl list = new MyListImpl();
public MyList getList() { return list; }
public void insert(Object o) { list.insert(o); }
}
Usage scenario:
使用场景:
A a = new A();
a.insert(123);
a.insert("quick brown fox");
MyList lst = a.getList();
System.out.println(lst.elementAt(0));
System.out.println(lst.elementAt(1));
回答by Aftaab Siddiki
Yes, you can pass calling object as an argument and put a check in insert() method before actual code.
是的,您可以将调用对象作为参数传递,并在实际代码之前检查 insert() 方法。
public void insert(Object obj){
if(obj instanceof A){
//your code block
}
}
Please note that this will allow all the classes that extends A as well to call insert. If you want to restrict only for class A, add additional check.
请注意,这将允许所有扩展 A 的类也调用插入。如果您只想限制 A 类,请添加额外的检查。
public void insert(Object obj){
if((obj instanceof A) && obj.getClass().getSimpleName().equals("A")){
//your code block
}
}
we can also achieve second case with only condition "obj.getClass().getSimpleName().equals("A")" as well.
我们也可以仅使用条件“obj.getClass().getSimpleName().equals("A")”来实现第二种情况。
回答by user1401472
package problems;
public class Problem1 {
public static void main(String[] args) {
B b = new B();
b.methodInB();
C c = new C();
c.methodNotInB();
}
}
class A {
public void onlyB() {
StackTraceElement[] stackTraceElements = Thread.currentThread()
.getStackTrace();
if (!problems.B.class.getCanonicalName().equals(
stackTraceElements[stackTraceElements.length - 2]
.getClassName())) {
System.err.println("You are not authorized to call me!!");
return;
}
System.out.println("You are authorized to call me!!");
}
}
class B {
public void methodInB() {
A a = new A();
a.onlyB();
}
}
class C {
public void methodNotInB() {
A a = new A();
a.onlyB();
}
}
回答by hvgotcodes
The best you can do using access modifiers is to make the method package private (remove the public
keyword) and keep only those two classes in the same package.
使用访问修饰符可以做的最好的事情是将方法包设为私有(删除public
关键字)并仅将这两个类保留在同一个包中。
回答by Shadi
Put it as inner class in A or you can do another thing ... Let insert takes one parameter with type of Object and in the beginning of it check if the parameter's type is A .... and when you call it send the calling object... maby its not a good idea but it will do what you want
把它作为内部类放在 A 中,或者你可以做另一件事......让 insert 接受一个类型为 Object 的参数,并在它的开头检查参数的类型是否为 A .... 当你调用它时发送调用对象... maby 这不是一个好主意,但它会做你想做的
回答by asteri
If all the "inner classes" stuff in the previous answers confuses you, there is another way that may be more intuitive (assuming you've learned about the extends
keyword and inheritance). You can simply make the insert()
method protected
instead of public
and make class A
extend List
. For example:
如果前面答案中的所有“内部类”内容都让您感到困惑,那么还有另一种可能更直观的方法(假设您已经了解了extends
关键字和继承)。您可以简单地 makeinsert()
方法protected
而不是public
make class A
extend List
。例如:
public class List {
...
protected void insert() {
//insertion occurs
}
...
}
public class A extends List {
...
}
As long as no other classes extend List, only objects of types A
and List
will ever be able to use the insert()
method.
只要没有其他类扩展列表中,只有对象的类型A
和List
永远都能够使用的insert()
方法。
回答by user207421
Put it into the permitted class and make it private.
将其放入允许的类并使其私有。