虚方法实现多态:virtual...override...new
using System;
public class Program
{
public static void Main()
{
Person P1 = new Person("name0");
Person w1 = new Woman("name1",29);
Person M1 = new Man("name2",12);
Person G1 = new Girl("name3",16);
P1.Animation();
w1.Animation();
M1.Animation();
G1.Animation();
Console.WriteLine("-------------End------------");
}
}
//虚方法
public class Person
{
public string _name;
public Person(string name){this._name=name;}
public virtual void Animation(){ Console.WriteLine("Person_Animation"); }
}
public class Woman: Person
{
public int _age;
public Woman(string name,int age):base(name){this._age=age; }
public override void Animation(){ Console.WriteLine("Woman_Animation"); }
}
public class Man: Person
{
public int _age;
public Man(string name,int age):base(name){this._age=age; }
public override void Animation(){ Console.WriteLine("Man_Animation"); }
}
public class Girl: Person
{
public int _age;
public Girl(string name,int age):base(name){this._age=age; }
public override void Animation(){ Console.WriteLine("Girl_Animation"); }
}
抽象类实现多态
using System;
public class Program
{
public static void Main()
{
//Person P1 = new Person("name0");
Person w1 = new Woman("name1",29);
Person M1 = new Man("name2",12);
Person G1 = new Girl("name3",16);
//P1.Animation();
w1.Animation();
M1.Animation();
G1.Animation();
Console.WriteLine("-------------End------------");
}
}
//抽象类
public abstract class Person
{
public string _name;
public Person(string name){this._name=name;}
public abstract void Animation() ;
//抽象方法不能有实现
//{ Console.WriteLine("Person_Animation"); }
}
public class Woman: Person
{
public int _age;
public Woman(string name,int age):base(name){this._age=age; }
public override void Animation(){ Console.WriteLine("Woman_Animation"); }
}
public class Man: Person
{
public int _age;
public Man(string name,int age):base(name){this._age=age; }
public override void Animation(){ Console.WriteLine("Man_Animation"); }
}
public class Girl: Person
{
public int _age;
public Girl(string name,int age):base(name){this._age=age; }
public override void Animation(){ Console.WriteLine("Girl_Animation"); }
}
接口实现多态:
using System;
public class Program
{
public static void Main()
{
//Person P1 = new Person("name0");
IAnimation w1 = new Woman("name1",29);
IAnimation M1 = new Man("name2",12);
IAnimation G1 = new Girl("name3",16);
//P1.Animation();
w1.Animation();
M1.Animation();
G1.Animation();
Console.WriteLine("-------------End------------");
}
}
public class Person
{
public string _name;
public Person(string name){this._name=name;}
//public abstract void Animation() ;
//{ Console.WriteLine("Person_Animation"); }
}
//接口
interface IAnimation
{
void Animation();
}
public class Woman: Person,IAnimation
{
public int _age;
public Woman(string name,int age):base(name){this._age=age; }
public void Animation(){ Console.WriteLine("Woman_Animation"); }
}
public class Man: Person,IAnimation
{
public int _age;
public Man(string name,int age):base(name){this._age=age; }
public void Animation(){ Console.WriteLine("Man_Animation"); }
}
public class Girl: Person,IAnimation
{
public int _age;
public Girl(string name,int age):base(name){this._age=age; }
public void Animation(){ Console.WriteLine("Girl_Animation"); }
}