保护
protected
关键字标记字段,方法属性和嵌套类,仅在同一个类和派生类中使用:
public class Foo()
{
protected void SomeFooMethod()
{
//do something
}
protected class Thing
{
private string blah;
public int N { get; set; }
}
}
public class Bar() : Foo
{
private void someBarMethod()
{
SomeFooMethod(); // inside derived class
var thing = new Thing(); // can use nested class
}
}
public class Baz()
{
private void someBazMethod()
{
var foo = new Foo();
foo.SomeFooMethod(); //not accessible due to protected modifier
}
}