编程经验:static,virtual,override,abstract四个修饰符区别--Demo

 

using System;

namespace Modifier

{

    public class GeneralMethods : staticMethods

    {

        ///<summary>

        ///一个尚未声明的静态字段"g_Y"实例化一个静态字段"g_X"

        ///</summary>

        public static int g_X = g_Y;

        ///<summary>

        ///"g_Y" 实例化。

        ///</summary>

        public static int g_Y = 5;

 

        public GeneralMethods()

        {

            //默认构造函数

        }

    }

 

    #region Static Methods

 

    ///<summary>

    ///  static 修饰符声明属于类型本身而不是属于特定对象的静态成员。static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,

    ///  但不能用于索引器、析构函数或类以外的类型。

    /// 

    ///  常数或者类型声明隐式地是静态成员。不能通过实例引用静态成员。然而,可以通过类型名称引用它。

    ///  尽管类的实例包含该类所有实例字段的单独副本,但每个静态字段只有一个副本。

    ///  不可以使用 this 来引用静态方法或属性访问器。

    ///  如果对类应用 static 关键字,则该类的所有成员都必须是静态的。

    ///   类(包括静态类)可以有静态构造函数。在程序开始和实例化类之间的某个时刻调用静态构造函数。

    ///</summary>

    public class staticMethods : abstractMethods

    {

        public string s_Id;

        public string s_Name;

 

        public override void Abstract_Method()

        {

            _as_x++;

            _as_y++;

        }

 

        public override int as_X   // overriding property

        {

            get

            {

                return _as_x + 10;

            }

        }

 

        public override int as_Y   // overriding property

        {

            get

            {

                return _as_y + 10;

            }

        }

 

        public staticMethods()

        {

            //默认构造函数

        }

        public staticMethods(string name, string id)

        {

            //自定义构造函数

            this.s_Name = name;

            this.s_Id = id;

        }

        //--  静态成员和方法,在外面这样访问:

        //--  GeneralMethods.s_employeeCounter

        //--  GeneralMethods.S_AddEmployee()

        ///<summary>

        ///未实例化,变量可以改变

        ///</summary>

        public static int s_employeeCounter;

        public static int S_AddEmployee()

        {

            //静态类,内部的成员必须是静态!

            return ++s_employeeCounter;

        }

}

 

    #endregion

 

    #region  Abstract Methods

 

    ///<summary>

    /// abstract 修饰符可以和类、方法、属性、索引器及事件一起使用。在类声明中使用 abstract 修饰符以指示此类(抽象类)只能是其他类的基类。标记为抽象或

    ///包含在抽象类中的成员必须通过从抽象类派生的类来实现。抽象类具有以下特性:

    /// 1抽象类不能实例化。

    /// 2、抽象类可以包含抽象方法和抽象访问器。

    /// 3、不能用 sealedC# 参考)修饰符修改抽象类,这意味着抽象类不能被继承。

    /// 4、从抽象类派生的非抽象类必须包括继承的所有抽象方法和抽象访问器的实实现。

    ///在方法或属性声明中使用 abstract 修饰符以指示方法或属性不包含实现。抽象方法具有以下特性:

    /// 1、抽象方法是隐式的虚方法。

    /// 2、只允许在抽象类中使用抽象方法声明。

    /// 3、因为抽象方法声明不提供实际的实现,所以没有方法体;方法声明只是以一个分号结束,并且在签名后没有大括号 ({ })

    /// 4、实现由一个重写方法overrideC# 参考)提供,此重写方法是非抽象类的一个成员。

    /// 5在抽象方法声明中使用 static virtual 修饰符是错误的。

    ///</summary>

    public abstract class abstractMethods : overrideMethods // Abstract class

    {

        /*

         * 除了在声明和调用语法上不同外,抽象属性的行为与抽象方法一样。

         * 在静态属性上使用 abstract 修饰符是错误的。

         * 在派生类中,通过包括使用 override 修饰符的属性声明,可以重写抽象的继承属性。

         * 抽象类必须为所有接口成员提供实现。

         */

        protected int _as_x = 100;

        protected int _as_y = 150;

        public abstract void Abstract_Method();   // Abstract method

        public abstract int as_X { get; }

        public abstract int as_Y { get; }

    }

 

    #endregion

 

    #region  Override Methods

 

    ///<summary>

    ///要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符。override 方法提供从基类继承的成员的新实现。通过 override 声明重写的方法称为重写基方法。

    ///重写的基方法必须与 override 方法具有相同的签名。不能重写非虚方法或静态方法。重写的基方法必须是 virtualabstract override 的。

    /// override 声明不能更改 virtual 方法的可访问性。override 方法和 virtual 方法必须具有相同的访问级别修饰符。不能使用修饰符 newstaticvirtual abstract 来修改 override 方法。

    ///重写属性声明必须指定与继承属性完全相同的访问修饰符、类型和名称,并且被重写的属性必须是 virtualabstract override 的。

    ///</summary>

    public class overrideMethods : virtualMethods

    {

        public class Employee

        {

            public string o_Name;

 

            // Basepay is defined as protected, so that it may be

            // accessed only by this class and derrived classes.

            protected decimal o_Basepay;

 

            // Constructor to set the name and basepay values.

            public Employee(string name1, decimal basepay1)

            {

                this.o_Name = name1;

                this.o_Basepay = basepay1;

            }

 

            // Declared virtual so it can be overridden.

            public virtual decimal O_CalculatePay()

            {

                return o_Basepay;

            }

        }

 

        // Derive a new class from Employee.

        public class SalesEmployee : Employee

        {

            // New field that will affect the base pay.

            private decimal o_Salesbonus;

 

            // The constructor calls the base-class version, and

            // initializes the salesbonus field.

            public SalesEmployee(string name, decimal basepay, decimal salesbonus): base(name, basepay)

            {

                //构造函数

                this.o_Salesbonus = salesbonus;

            }

 

            // Override the CalculatePay method

            // to take bonus into account.

            public override decimal O_CalculatePay()

            {

                return o_Basepay + o_Salesbonus;

            }

        }

    }

 

    #endregion

 

    #region  Virtual Methods

    ///<summary>

    /// virtual 关键字用于修饰方法、属性、索引器或事件声明,并且允许在派生类中重写这些对象。例如,此方法可被任何继承它的类重写。虚拟成员的实现可由派生类中的重写成员更改。

    ///

    ///调用虚方法时,将为重写成员检查该对象的运行时类型。将调用大部分派生类中的该重写成员,如果没有派生类重写该成员,则它可能是原始成员。默认情况下,方法是非虚拟的。不能重写非虚方法。

    /// virtual 修饰符不能与 staticabstract, private override 修饰符一起使用。除了声明和调用语法不同外,虚拟属性的行为与抽象方法一样。

    /// 1在静态属性上使用 virtual 修饰符是错误的

    /// 2、通过包括使用 override 修饰符的属性声明,可在派生类中重写虚拟继承属性。

    ///</summary>

    public class virtualMethods

    {

        public class Dimensions

        {

            public const double v_PI = Math.PI;//系统常量 PI = 3.14159;

            protected double v_x, v_y;

            public Dimensions()

            {

            }

            public Dimensions(double x, double y)

            {

                this.v_x = x;

                this.v_y = y;

            }

 

            public virtual double Area()

            {

                return v_x * v_y;

            }

        }

 

        public class Circle : Dimensions

        {

            public Circle(double r)

                : base(r, 0)

            {

                //构造函数

            }

 

            public override double Area()

            {

                return v_PI * v_x * v_x;

            }

        }

 

        public class Sphere : Dimensions

        {

            public Sphere(double r)

                : base(r, 0)

            {

            }

 

            public override double Area()

            {

                return 4 * v_PI * v_x * v_x;

            }

        }

 

        public class Cylinder : Dimensions

        {

            public Cylinder(double r, double h)

                : base(r, h)

            {

            }

 

            public override double Area()

            {

                return 2 * v_PI * v_x * v_x + 2 * v_PI * v_x * v_y;

            }

        }

    }

 

    #endregion

 

    class MainClass : GeneralMethods

    {

        static void Main()

        {

            ////////////////////////////////////////////////General  Methods/////////////////////////////////////////////////

            Console.WriteLine("////////////////////////General Methods////////////////////////");

            Console.WriteLine("");

 

            Console.WriteLine(GeneralMethods.g_X);

            Console.WriteLine(GeneralMethods.g_Y);

            GeneralMethods.g_X = 99;//可以重新赋值

            GeneralMethods.g_Y = 80;//可以重新赋值

            Console.WriteLine(GeneralMethods.g_X);

            Console.WriteLine(GeneralMethods.g_Y);

            Console.WriteLine("");

            ////////////////////////////////////////////////Static   Methods/////////////////////////////////////////////////

            Console.WriteLine("////////////////////////Static   Methods////////////////////////");

            Console.WriteLine("");

 

            Console.Write("Enter the employee's name: ");

            string name = Console.ReadLine();

            Console.Write("Enter the employee's ID: ");

            string id = Console.ReadLine();

            // Create and configure the employee object:

            staticMethods sm = new staticMethods(name, id);

            Console.Write("Enter the current number of employees: ");

            string n = Console.ReadLine();

            staticMethods.s_employeeCounter = Int32.Parse(n);

            staticMethods.S_AddEmployee();

            // Display the new information:

            Console.WriteLine("Name: {0}", sm.s_Name);

            Console.WriteLine("ID:   {0}", sm.s_Id);

            Console.WriteLine("New Number of Employees: {0}", staticMethods.s_employeeCounter);

 

            Console.WriteLine("");

            ////////////////////////////////////////////////Abstract Methods/////////////////////////////////////////////////

            Console.WriteLine("////////////////////////Abstract Methods////////////////////////");

            Console.WriteLine("");

 

            staticMethods am = new staticMethods();

            am.Abstract_Method();

            Console.WriteLine("x = {0}, y = {1}", am.as_X, am.as_Y);

 

            Console.WriteLine("");

            ////////////////////////////////////////////////Override Methods/////////////////////////////////////////////////

            Console.WriteLine("////////////////////////Override Methods////////////////////////");

            Console.WriteLine("");

 

            // Create some new employees.

            SalesEmployee employee1 = new SalesEmployee("Alice",1000, 500);

            Employee employee2 = new Employee("Bob", 1200);

            Console.WriteLine("Employee " + employee1.o_Name +" earned: " + employee1.O_CalculatePay());

            Console.WriteLine("Employee " + employee2.o_Name +" earned: " + employee2.O_CalculatePay());

 

            Console.WriteLine("");

          

            ////////////////////////////////////////////////Virtual  Methods/////////////////////////////////////////////////

            Console.WriteLine("////////////////////////Virtual  Methods////////////////////////");

            Console.WriteLine("");

 

            double r = 3.0, h = 5.0;

            Dimensions vm_c = new Circle(r);

            Dimensions vm_s = new Sphere(r);

            Dimensions vm_l = new Cylinder(r, h);

            // Display results:

            Console.WriteLine("Area of Circle   = {0:F2}", vm_c.Area());

            Console.WriteLine("Area of Sphere   = {0:F2}", vm_s.Area());

            Console.WriteLine("Area of Cylinder = {0:F2}", vm_l.Area());

        }

    }

}

static,virtual,override,abstract四个修饰符区别

posted @ 2013-06-13 14:33  信息无障碍  阅读(233)  评论(0编辑  收藏  举报