基础学习:继承的学习-2020.12.08更新

今天学习C# 如何使用继承

构建了一个基类Mammal;构建了一个子类Horse;同时在基类和子类中创建了构造函数,在运行子类Horse实例的时候,编译器先调用基类的构造函数并进行了输出;再调用子类的构造函数并输出;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApp76
 8 {
 9     //基类
10     public class Mammal
11     {
12         public Mammal()
13         {
14             Console.WriteLine("i am 基类构造函数");
15         }
16 
17         public void Sing()
18         {
19             Console.WriteLine("I am a Animal!");
20         }
21     }
22     //派生类
23     public class Horse : Mammal
24     {
25         public  Horse()
26         {
27             Console.WriteLine("i am 派生类构造函数!");
28         }
29 
30         public void Jiao()
31         {
32             Console.WriteLine("I am a Ma!");
33         }
34     }
35     class Program
36     {
37       static void Main(string[] args)
38         {
39             Horse horse = new Horse();
40             
41         }
42     }
43 }
View Code

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在子类和基类中定义签名相同的方法Sing(),并使用horse.sing();进行调用,输出结果是子类的方法中的内容;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApp76
 8 {
 9     //基类
10     public class Mammal
11     {
12         public Mammal()
13         {
14             Console.WriteLine("i am 基类构造函数");
15         }
16 
17         public void Sing()
18         {
19             Console.WriteLine("I am a Animal!");
20         }
21     }
22     //派生类
23     public class Horse : Mammal
24     {
25         public  Horse()
26         {
27             Console.WriteLine("i am 派生类构造函数!");
28         }
29 
30         public void Sing()
31         {
32             Console.WriteLine("I am a Ma!");
33         }
34     }
35     class Program
36     {
37       static void Main(string[] args)
38         {
39             Horse horse = new Horse();
40             horse.Sing();
41             
42         }
43     }
44 }
View Code

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

只要是子类进行实例化,无论子类有无显示地构造函数,都会调用基类的构造函数,如下面的Cat子类此示:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApp76
 8 {
 9     //基类
10     public class Mammal
11     {
12         public Mammal()
13         {
14             Console.WriteLine("i am 基类构造函数");
15         }
16 
17         public virtual  void Sing()
18         {
19             Console.WriteLine("I am a Animal!");
20         }
21     }
22     //派生类
23     public class Horse : Mammal
24     {
25         public  Horse()
26         {
27             Console.WriteLine("i am 派生类构造函数!");
28         }
29 
30         public override  void Sing()
31         {
32             Console.WriteLine("I am a Ma!");
33         }
34     }
35 
36     public class Cat :Mammal
37     {
38         public  string GetName()
39         {
40             return "没事喝点茶!";
41         }
42     }
43     class Program
44     {
45       static void Main(string[] args)
46         {
47             //Mammal myMammal;
48             //Horse myHorse = new Horse();
49 
50             Cat myCat = new Cat();
51 
52             //myMammal = myHorse;
53             //myMammal.Sing();
54 
55             //myMammal = myCat;
56             //myMammal.Sing();
57         }
58     }
59 }
View Code

子类并没有继承父类的构造函数,但是,子类会默认的调用父无参数的构造函数,创建父类对象,让子类可以使用父类中的成员。

posted @ 2020-03-14 16:23  chenlight  阅读(168)  评论(0)    收藏  举报