是否有一个访问修饰符,允许继承的类修改c#中的父类成员?

nik*_*sfi 1 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

它比这里的示例代码稍微复杂一点,但我想我遇到了问题.

mar*_*c_s 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
}
Run Code Online (Sandbox Code Playgroud)