继承

1、父类的所有成员都是子类必须的,只要有一个不是子类想要的就不存在继承关系

 


2、C#里面的类只能有一个父类 单根性
3、类继承具有传递性 传递性
4、C#里面的类默认直接或者间接继承于Object类
5、创建子类对象的时候先调用子类的构造函数 再调用子类的构造函数
6、子类的构造函数默认调用父类的无参数构造函数
7、base关键字代表父类对象
8、this关键字代表当前对象
9、base显示调用父类成员
10、当子类对象中有和父类相同的成员的时候,子类对象的同名尘缘会覆盖父类中的同名成员
11、new显式的隐藏父类成员
12、子类的访问级别不能比父类高

Person类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 继承
 7 {
 8     class Person
 9     {
10         public int Age { get; set; }
11         public string Name { get; set; }
12         protected double Height { get; set; }
13         //protected只能在本类和本类的子类中访问.   
14         private int money = 110; 
15         public Person(string name, int age, double height)
16         {
17             Console.WriteLine("Person类的构造函数被调用啦....");
18         }
19 
20         public int GetMoney()
21         {
22             return this.money;
23         }
24 
25         public void Test()
26         {
27             Console.WriteLine("父类的Test方法...");
28         } 
29 
30     }
31 }

 

Student类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 继承
 7 {
 8 
 9     //子类从父类继承所有的非私有成员
10     //C#里继承可不可以多继承 
11     //C#里的类只能有1个父类 单根性
12     class Student : Person
13     { 
14         public string StuNo { get; set; }
15         public new int Age { get; set; } 
16         //子类和父类的关系 父类的所有的成员都是子类必须的.
17         //只要有1个不是子类想要的 就不存在继承关系  
18         //包含的关系 人  中国人 
19         //C#中所有的类都只直接或者间接的继承于 Object类 
20         //子类的构造函数 默认 会调用父类的 无参数的构造函数...
21         //base关键字代表父类对象
22         //this关键字代表当前对象
23         //public Student():base()
24         //{
25         //    Console.WriteLine("学生类的构造函数被调用了....");
26         //}
27 
28 
29         //子类构造函数调用父类构造函数的语法
30         public Student(string name, int age, double height)
31             : base(name, age, height)
32         {
33 
34         }
35 
36 
37         //new关键字 显示的隐藏父类成员
38         public new void Test()
39         {
40             Console.WriteLine("子类的Test方法...");
41         }
42 
43     }
44 }

内存分配初略

 

money虽在父类中式私有成员,但是有公共的方法中能得到,所以在实例化Student类的时候,money字段会加进去

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 继承_值类型_引用类型
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Student mystu = new Student("ppp");
14             Student mystus = new Student();
15             Console.ReadLine();
16         }
17     }
18     public class Student:Person
19     {
20         public Student()
21         {
22             Console.WriteLine("Student构造函数,我继承于Person类");
23         }
24         public Student(string temp):base(temp)
25         {
26             Console.WriteLine("Student(带参数)构造函数,我继承于Person类,参数{0},但是不调用Human带参数构造函数", temp);
27         }
28         public new void SayHi()
29         {
30             Console.WriteLine("Student说hello");
31         }
32     }
33     public class Person:Human
34     {
35         //string temp = "我掉";
36         public Person(string temp)
37             : base()
38         {
39             Console.WriteLine("Person(带参数)构造函数,我继承于Human类,参数{0}",temp);
40             
41         }
42         public Person()
43         {
44             Console.WriteLine("Person构造函数,我继承于Human类");
45         }
46         public new void SayHi()
47         {
48             Console.WriteLine("Person说hello");
49         }
50     }
51     public class Human
52     {
53         public Human()
54         {
55             Console.WriteLine("Human构造函数");
56         }
57         public Human(string temp)
58         {
59             Console.WriteLine("Human(带参数)的构造函数,传的值是{0}",temp);
60 
61         }
62         public void SayHi()
63         {
64             Console.WriteLine("Human说hello");
65         }
66 
67     }
68 }

posted on 2012-12-08 23:23  陈谨  阅读(191)  评论(0编辑  收藏  举报