【发布时间】:2012-07-18 20:06:17
【问题描述】:
这是一个例子:
class A
{
List l = new List ();
list.insert("x");
}
class List
{
...
public void insert ()
{
/*insertion occurs*/
}
...
}
是否有可能将 insert() 方法保持为公共状态,但只限制对类 A 的访问,以便只有在从 A 调用时,其他类才能访问它?
【问题讨论】:
-
您可以将
List设为A中的内部类,因此其他类将无法访问它。其他方式可能是设置List#insert(A a)