*里氏替換原則實(shí)現(xiàn)方法:子類可以出現(xiàn)在任何父類
?*?
?* 動(dòng)態(tài)多動(dòng)性:
?* 1.虛方法的多態(tài)
?* 2.抽象方法的多態(tài)
?* 3.接口方法的動(dòng)態(tài)多動(dòng)性
?*?
?* 動(dòng)態(tài)多動(dòng)性的作用:
?* 1.實(shí)現(xiàn)功能定義與功能實(shí)現(xiàn)香脫離伊诵,實(shí)現(xiàn)代碼進(jìn)一步靈活性
?* 2.實(shí)現(xiàn)系統(tǒng)設(shè)計(jì)與系統(tǒng)代碼想脫離单绑,實(shí)現(xiàn)代碼穩(wěn)定性,減少系統(tǒng)BUG
?*?
class Person
{
public virtual void Speak(){
Console.WriteLine("父類說");
}
}
class Zhang:Person
{
public override void Speak ()
{
Console.WriteLine("張說");
}
}
interface IPerson
{
void Speak ();
}
interface IPersontwo
{
void Speak ();
}
class Chinese:IPerson,IPersontwo
{
void IPerson.Speak()
{
Console.WriteLine ("中國人說中國話");
}
void IPersontwo.Speak()
{
Console.WriteLine ("中國人還會(huì)說英語");
}
}
class MainClass
{
public static void Main (string[] args)
{
Person p = new Zhang ();
p.Speak ();
//動(dòng)態(tài)多態(tài)性之接口
Chinese c=new Chinese();
((IPerson)c).Speak ();
IPerson ip = new Chinese ();
ip.Speak ();
IPersontwo ip2 = new Chinese ();
ip2.Speak ();
}
}
代碼設(shè)計(jì)結(jié)構(gòu)
//代表發(fā)郵件的功能
interface IEmailSender
{
void Send(Customer customer);
}
abstract class Customer:IEmailSender
{
public string name;
public string eamil;
abstract public void Send(Customer customer);
}
class CommonCustomer:Customer
{
public override void Send(Customer customer){
Console.WriteLine("普通用戶發(fā)郵件" + customer.name);
}
}
class VipCustomer:Customer
{
public override void Send(Customer customer){
Console.WriteLine("vip用戶發(fā)郵件" + customer.name);
}
}
class MainClass
{
public static void Main (string[] args)
{
//創(chuàng)建普通用戶
CommonCustomer cc = new CommonCustomer();
cc.name = "普通用戶A";
cc.Send (cc);
//創(chuàng)建vip
VipCustomer vc = new VipCustomer();
vc.name = "Vip用戶";
vc.Send (vc);
}
}