关于基类中函数修饰符abstract和virtual的使用


关于基类中函数修饰符abstract和virtual的使用


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer.Qualifier
{
    //关于基类中函数修饰符abstract和virtual的使用
    class Program
    {
        static void Main(string[] args)
        {
            //V1
            //Hint: The best way is just by sony or lenove//第一个版本V1我使用此方式

            //V2
            //开始为了写普通方法->简单工厂->工厂方法->抽象工厂->......
            //竟然写着写着就写到多态了,多态和重载不是一个概念,重载是指同名函数的参数类型不同或者参数个数不同,与返回类型无关。
            //而多态是指同名函数,不但参数类型和参数个数相同,还有返回类型也相同,只是函数的修饰符不同。
            //最终决定调用基类还是子类,abstract/virtual/override/new要比上面的情况复杂。
            //注释中的"//right"只是说在通常情况下使用最多的方式,“//bad”则反之。

            //Hint: The best way is just by Dell or IBM//第二个版本V2,后面几篇文都是基于这个讲解,所以在这个版本我把修饰符换了

            PC pc;
            //pc.Name = "NA PC";
            //pc = new Dell("");
            pc = new Dell("Dell");
            pc.Name = "OtherDell";
            pc.Describe();


            System.Console.WriteLine();
            pc = new IBM("IBM");
            pc.Name = "OtherIBM";
            pc.Describe();

            System.Console.WriteLine();
            pc = new HP("HP");
            pc.Name = "OtherHP";
            pc.Describe();

            System.Console.WriteLine();
            pc = new Sony("Sony");
            pc.Name = "OtherSony";
            pc.Describe();


            System.Console.WriteLine();
            pc = new Lenovo("Lenovo");
            pc.Name = "OtherLenovo";
            pc.Describe();
        }
    }

    abstract class PC//:Brand
    {
        //注意这篇文章中这里使用了protected,和后面文章中的改用成private有很大的不同
        protected string name;

        //public PC(string name){ } //bad, sub-class need tranfer this.name=name

        public PC(string name)      //right, sub-calss needn't tranfer this.name=name
        {
            //this.name = "unknown";   
            this.name = name;
            //this.name = !string.IsNullOrEmpty(name) ? name : "unknown";
        }

        //The super-class property(Name{get;set;}) don't use abstract,otherwise the sub-class(Dell/IBM/...) must implement Name{get;set;}
        //public abstract string Name { get; set; } //bad
        public virtual string Name { get; set; }    //right        

        //public abstract void Describe();       //right 
        public virtual void Describe() { System.Console.WriteLine("I am {0} personal computer of super-calss", name); }    //bad
    }

    class Dell : PC
    {
        public Dell(string name) : base(name)
        {
            //this.name = name; //no use tranfer
        }

        public override void Describe()
        {
            System.Console.WriteLine("I am {0} personal computer", name);
            System.Console.WriteLine("I am {0} personal computer", Name);
        }
    }

    class IBM : PC
    {
        protected new string name;  //No, or have to use it(要么不用,要么必须这样用),此处的成员变量(name)只能这样new基类同名成员变量

        //the qualifier(override/new) don't use new, otherwise will call super-class
        public override string Name { get; set; }    //right, use override, it's call sub-class

        public IBM(string name) : base(name)
        {
            this.name = name;
        }

        public override void Describe()             //right, use override, it's call sub-class
        {
            System.Console.WriteLine("I am {0} personal computer", name);
            System.Console.WriteLine("I am {0} personal computer", Name);
        }
    }

    class HP : PC
    {
        protected new string name;
        public override string Name { get; set; }   //right

        public HP(string name) : base(name)
        {
            this.name = name;
        }

        
        public new void Describe()                  //bad
        {
            System.Console.WriteLine("I am {0} personal computer", name);
            System.Console.WriteLine("I am {0} personal computer", Name);
        }
    }

    class Sony : PC
    {
        protected new string name;
        public new string Name { get; set; }        //bad

        public Sony(string name) : base(name)
        {
            this.name = name;
        }

        public override void Describe()             //right
        {
            System.Console.WriteLine("I am {0} personal computer", name);
            System.Console.WriteLine("I am {0} personal computer", Name);
        }
    }

    class Lenovo : PC
    {
        protected new string name;
        public new string Name { get; set; }        //bad

        public Lenovo(string name) : base(name)
        {
            this.name = name;
        }

        public new void Describe()                  //bad
        {
            System.Console.WriteLine("I am {0} personal computer", name);
            System.Console.WriteLine("I am {0} personal computer", Name);
        }
    }
}




版权所有,转载请注明文章出处 http://blog/csdn.net/cadenzasolo



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章