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);
}
}
}
關於基類中函數修飾符abstract和virtual的使用
關於基類中函數修飾符abstract和virtual的使用
版權所有,轉載請註明文章出處
http://blog/csdn.net/cadenzasolo
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.