*里氏替换原则实现方法:子类可以出现在任何父类
*
* 动态多动性:
* 1.虚方法的多态
* 2.抽象方法的多态
* 3.接口方法的动态多动性
*
* 动态多动性的作用:
* 1.实现功能定义与功能实现香脱离,实现代码进一步灵活性
* 2.实现系统设计与系统代码想脱离,实现代码稳定性,减少系统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 ("中国人还会说英语");
}
}
class MainClass
{
public static void Main (string[] args)
{
Person p = new Zhang ();
p.Speak ();
//动态多态性之接口
Chinese c=new Chinese();
((IPerson)c).Speak ();
IPerson ip = new Chinese ();
ip.Speak ();
IPersontwo ip2 = new Chinese ();
ip2.Speak ();
}
}
代码设计结构
//代表发邮件的功能
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("普通用户发邮件" + customer.name);
}
}
class VipCustomer:Customer
{
public override void Send(Customer customer){
Console.WriteLine("vip用户发邮件" + customer.name);
}
}
class MainClass
{
public static void Main (string[] args)
{
//创建普通用户
CommonCustomer cc = new CommonCustomer();
cc.name = "普通用户A";
cc.Send (cc);
//创建vip
VipCustomer vc = new VipCustomer();
vc.name = "Vip用户";
vc.Send (vc);
}
}