面向对象三大特征之一多态

多态-不同对象作用于相同方法,呈现的结果不同

表现形式为A类 对象名=new B类() A类和B类之间存在直接或间接的继承关系,A类

叫做申明类,B类叫做实例类

 

运行时多态:重载(overload)

编译时多态:重写(override)

 

重载:方法名相同,形参的类型个数顺序不同,只与形参有关,与返回值无关

 1 class People
 2 {
 3     public int Age { get; set; }
 4     
 5     public string Name { get; set; }
 6     
 7     public People()
 8     {
 9     }
10     
11     public People(string name,int age)
12     {
13         this.Name = name;
14         this.Age = age;
15     }
16     
17     public void Eat()
18     {
19     }
20     
21     public void Eat(string s,int i)
22     {
23     }
24     
25     public void Eat(int i,string s)
26     {
27     }
28 }
View Code

重写:继承父类的虚方法、抽象方法、override修饰的方法或实现接口

 1 class People
 2 {
 3     public int Age { get; set; }
 4     
 5     public string Name { get; set; }
 6     
 7     public People()
 8     {
 9     }
10     
11     public People(string name,int age)
12     {
13         this.Name=name;
14         this.Age=age;
15     }
16     
17     public virtual void SayHi()
18     {
19         Console.WriteLine("{0},你好!",Name);
20     }
21 }
22 
23 class Student:People
24 {
25     public override void SayHi()
26     {
27         Console.WriteLine("{0}学生好",Name);
28     }
29 }
View Code
原文地址:https://www.cnblogs.com/arvinzd/p/14172360.html