是否有一个访问修饰符,它允许继承类修改c#中父类的成员?
问题描述:
有没有办法限制评论中概述的这些类的成员的访问权限;是否有一个访问修饰符,它允许继承类修改c#中父类的成员?
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;
}
}
这是比这里的示例代码更复杂一点,但我认为我得到了我的问题。
答
是 - 那将是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
}
马克