C# 基础入门 互动版

protected


  protected 访问修饰符允许子类访问它的基类的成员变量和成员函数。

  protected类型并不是能够被派生类所访问就是随心所欲的访问,而是有条件的,访问必须是通过派生类类型发生时,在派生类中的基类的protected类型成员才能够被访问。

class BaseTest
{
  public int a = 10;
  protected int b = 2;
}
class ChildTest : BaseTest
{
  int c;
  int d;
  static void Main(string[] args)
  {
    ChildTest childTest = new ChildTest(); //声明派生类
    Console.WriteLine(childTest.b);
  }
}