protected void update()
  {
    ……
  }
protected 里面的变量或对象能不能被其它函数调用?

解决方案 »

  1.   

    To: y7967(℃遥遥.NET) 
    还是不是很明白,能不能进一步解释一下?Thanks
      

  2.   

    摘于MSDNprotected 关键字是一个成员访问修饰符。从声明受保护的成员的类中,以及从声明受保护的成员的类派生的任何类中都可以访问该成员。仅当访问通过派生类类型发生时,基类的受保护成员在派生类中才是可访问的。例如,请看以下代码段:class A 
    {
       protected int x = 123;
    }class B : A 
    {
       void F() 
       {
          A a = new A();  
          B b = new B();  
          a.x = 10;   // Error
          b.x = 10;   // OK
       }
    }
    语句 a.x =10 将生成错误,因为 A 不是从 B 派生的。结构成员无法受保护,因为无法继承结构。对于不是从受保护的成员的类派生的类,引用其中受保护的成员是错误的。示例
    在此示例中,类 MyDerivedC 从 MyClass 派生;因此,可以从该派生类直接访问基类的受保护成员。// protected_keyword.cs
    using System;
    class MyClass 
    {
       protected int x; 
       protected int y;
    }class MyDerivedC: MyClass 
    {
       public static void Main() 
       {
          MyDerivedC mC = new MyDerivedC();      // Direct access to protected members:
          mC.x = 10;
          mC.y = 15;
          Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); 
       }
    }
    输出
    x = 10, y = 15
    如果将 x 和 y 的访问级别更改为 private,编译器将发出错误信息:'MyClass.y' is inaccessible due to its protection level.
    'MyClass.x' is inaccessible due to its protection level.