C# 可访问性级别:受保护和私有的区别是什么

C# 可访问性级别:受保护和私有的区别是什么,c#,oop,C#,Oop,你能帮我理解私有方法和受保护方法的区别吗。 下列的 我无法理解它,特别是短语Private:Access仅限于包含类型 谢谢你的帮助。再见 私有成员只能对包含类型(即类本身)可见 class Test { private method myMethod() {} protected method myprotectedMethod() {} } class ChildTest : Test { public method useProtectedBaseMethod

你能帮我理解私有方法和受保护方法的区别吗。 下列的 我无法理解它,特别是短语Private:Access仅限于包含类型


谢谢你的帮助。再见

私有成员只能对包含类型(即类本身)可见

class Test
{
  private method myMethod()
   {}
  protected method myprotectedMethod()
   {}
}


class ChildTest : Test
{
  public method useProtectedBaseMethod ()
  {
     this.myProtectedMethod(); // this is ok
     this.myMethod(); // this is NOT ok. will throw an Error
  }
}

class outsider
{
  Test  objTest = new Test();
  objTest.myProtectedMethod(); // throws an error as it is not accessible
  objTest.myMethod(); // throws an error as it is not accessible
}
受保护的成员可由包含类和派生类私有访问-仅在类的作用域中可见

受保护-对类的继承者可见


protected可以与internal结合使用:如果您从所讨论的类继承,则在同一程序集中或任何其他程序集中都可以看到类成员。

您好,很抱歉我犯了一个错误。。我想知道受保护vs私有。好吧,这与ASP.NET无关,只是为了让我明白。受保护成员的访问范围比私有的??错误的–受保护的内部成员更广,这意味着它可以从同一程序集或继承者访问。斯维克是对的……受保护的内部成员并不意味着只允许在同一程序集中进行继承。是的,没错,看起来我没有注意到。@GIbboK-很高兴有用处:-