继承-virtual,override
virtual(虚拟)
1、允许在派生类中重写基类的方法,一般情况下方法是非虚拟的,不能重写。
2、virtual不能与static修饰符一起使用。
override(重写)
1、override提供从基类继承的成员的新实现,通过override声明、重写方法称之为重写基方法。
2、重写的基方法必须与override方法具有相同的签名,不能重写非虚的方法和静态的方法。
3、重写的基方法必须是virtual、abstract(抽象)和override.
1class Program
2 {
3 static void Main(string[] args)
4 {
5 People lm = new People();
6 lm.info("李明", 20);
7 Boy xm = new Boy();
8 xm.info("小明",23);
9 girl xh = new girl();
10 xh.info("小红",22);
11 }
12 }
13class People
14 {
15 protected string name;
16 protected uint age;
17 public virtual void info(string uname, uint uage)
18 {
19 name = uname;
20 age = uage;
21 Console.WriteLine("姓名:{0},年龄:{1}", name, age);
22 }
23 }
24 class Boy : People
25 {
26 public override void info(string uname, uint uage)
27 {
28 name = uname;
29 age = uage;
30 string sex = "男";
31 Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", name, age, sex);
32
33 }
34 }
35 class girl : People
36 {
37 public override void info(string uname, uint uage)
38 {
39 name = uname;
40 age = uage;
41 string sex = "女";
42 Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", name, age, sex);
43
44 }
45 }
2 {
3 static void Main(string[] args)
4 {
5 People lm = new People();
6 lm.info("李明", 20);
7 Boy xm = new Boy();
8 xm.info("小明",23);
9 girl xh = new girl();
10 xh.info("小红",22);
11 }
12 }
13class People
14 {
15 protected string name;
16 protected uint age;
17 public virtual void info(string uname, uint uage)
18 {
19 name = uname;
20 age = uage;
21 Console.WriteLine("姓名:{0},年龄:{1}", name, age);
22 }
23 }
24 class Boy : People
25 {
26 public override void info(string uname, uint uage)
27 {
28 name = uname;
29 age = uage;
30 string sex = "男";
31 Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", name, age, sex);
32
33 }
34 }
35 class girl : People
36 {
37 public override void info(string uname, uint uage)
38 {
39 name = uname;
40 age = uage;
41 string sex = "女";
42 Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", name, age, sex);
43
44 }
45 }