C# Keyword(base,protected, override,….)

來源:互聯網
上載者:User

(1)protected

      The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

    //proteted修飾的方法 (本類  和 衍生類別 內部可訪問)

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   }}

(2)base

    The base keyword is used to access members of the base class from within a derived class:  

  • Call a method on the base class that has been overridden by another method.
  • Specify which base-class constructor should be called when creating instances of the derived class.

     (用於訪問基類的成員,僅限於在衍生類別內部.  base.方法名())

A base class access is permitted only in a constructor, an instance method, or an instance property accessor.

   (只可在衍生類別(derived class)的 建構函式,執行個體方法,執行個體屬性  中用base訪問基類)

// keywords_base.cs// Accessing base class membersusing System;   public class Person    {      protected string ssn = "444-55-6666";      protected string name = "John L. Malgraine";      public virtual void GetInfo()       {         Console.WriteLine("Name: {0}", name);         Console.WriteLine("SSN: {0}", ssn);      }   }   class Employee: Person    {      public string id = "ABC567EFG";      public override void GetInfo()       {         // Calling the base class GetInfo method:         base.GetInfo();         Console.WriteLine("Employee ID: {0}", id);      }   }class TestClass {   public static void Main()    {      Employee E = new Employee();      E.GetInfo();   }}

結果:

    Name:John L. Malgraine

    SSN:444-55-6666

    Employee ID:ABC567EFG

(3) virtual -overrid (必配套使用)

    基類中用virtual修飾的方法,在衍生類別中可用override重寫

class TestClass{    public class Shape    {        public const double PI = Math.PI;        protected double x, y;        public Shape()        {        }        public Shape(double x, double y)        {            this.x = x;            this.y = y;        }        public virtual double Area()        {            return x * y;        }    }    public class Circle : Shape    {        public Circle(double r) : base(r, 0)        {        }        public override double Area()        {            return PI * x * x;        }    }    class Sphere : Shape    {        public Sphere(double r) : base(r, 0)        {        }        public override double Area()        {            return 4 * PI * x * x;        }    }    class Cylinder : Shape    {        public Cylinder(double r, double h) : base(r, h)        {        }        public override double Area()        {            return 2 * PI * x * x + 2 * PI * x * y;        }    }    static void Main()    {        double r = 3.0, h = 5.0;        Shape c = new Circle(r);        Shape s = new Sphere(r);        Shape l = new Cylinder(r, h);        // Display results:        Console.WriteLine("Area of Circle   = {0:F2}", c.Area());        Console.WriteLine("Area of Sphere   = {0:F2}", s.Area());        Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());        }    }    /*        Output:        Area of Circle   = 28.27        Area of Sphere   = 113.10        Area of Cylinder = 150.80    */

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.