有没有办法限制对这些类成员的访问,如评论中所述;
class a
{
int p //should be accessable by b,c, but not by x
}
class b:a
{
int q //should be accessable by c, if it has to by a, but not by x
}
class c:b
{
public int r //obviously accessable by anyone
}
class x
{
c testfunction()
{
c foo=new c();
foo.r=20;
return foo;
}
}
它比这里的示例代码稍微复杂一些,但我认为我遇到了问题。
答案 0 :(得分:11)
是 - 这将是protected
访问修饰符 - 允许后代访问它,但不允许“外部”用户访问。
class a
{
protected int p //should be accessable by b,c, but not by x
}
class b:a
{
protected int q //should be accessable by c, if it has to by a, but not by x
}
class c:b
{
public int r //obviously accessable by anyone
}
马克