New:
- 隐藏数据成员
- 隐藏方法成员
- 隐藏静态成员
New关键字在类的继承时,子类的构造函数中可以起到隐藏基类【数据成员】、【方法成员】和【静态成员】的作用。说白了就是类似于重写功能,比如:
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace T1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Student s = new Student(); 13 Console.WriteLine(s.Name); 14 Console.WriteLine(s.Age); 15 s.s(); 16 } 17 } 18 19 class Person 20 { 21 public string Name = "ji"; 22 public int Age = 1; 23 24 public void s() 25 { 26 Console.WriteLine("ji"); 27 } 28 } 29 30 class Student : Person 31 { 32 33 34 } 35 }
19: 新建Person类,有2个【数据成员】和一个【方法成员】
30:Student类继承与Person
Main函数:输出Student类的 数据成员和方法成员;
结果:
若改成:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace T1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Student s = new Student(); 13 Console.WriteLine(s.Name); 14 Console.WriteLine(s.Age); 15 s.s(); 16 } 17 } 18 19 class Person 20 { 21 public string Name = "ji"; 22 public int Age = 1; 23 24 public void s() 25 { 26 Console.WriteLine("ji"); 27 } 28 } 29 30 class Student : Person 31 { 32 new public string Name; 33 new public int Age = 2; 34 new public void s() 35 { 36 Console.WriteLine("er"); 37 } 38 } 39 }
32~34: 隐藏了基类中的【数据成员】和【方法成员】,且重写;
new public string Name = new public string Name = null ;
结果:
Base:
- 制指定积累的某个构造函数
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace T1 7 { 8 class T2 9 { 10 static void Main(string[] args) 11 { 12 Student s = new Student("base"); 13 } 14 15 } 16 class Person 17 { 18 public Person(string str) 19 { 20 Console.WriteLine("我是基类" + str); 21 } 22 public Person() 23 { 24 Console.WriteLine("`````"); 25 } 26 } 27 28 class Student : Person 29 { 30 public Student(string str) 31 : base(str) 32 { 33 Console.WriteLine("我是子类" + str); 34 } 35 36 public Student() 37 { 38 Console.WriteLine("```b``"); 39 } 40 } 41 }
结果:
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace T1 7 { 8 class T2 9 { 10 static void Main(string[] args) 11 { 12 Student s = new Student("base"); 13 } 14 15 } 16 class Person 17 { 18 public Person() 19 { 20 Console.WriteLine("`````"); 21 } 22 23 public Person(string str) 24 { 25 Console.WriteLine("我是基类" + str); 26 } 27 28 } 29 30 class Student : Person 31 { 32 33 public Student() 34 { 35 Console.WriteLine("```b``"); 36 } 37 38 public Student(string str) 39 //: base(str) 40 { 41 Console.WriteLine("我是子类" + str); 42 } 43 44 45 } 46 }
结果:
可见base的作用就是在子类中选定构造函数;只执行选中的构造函数,不执行默认的构造函数;
This:
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace T1 7 { 8 class T2 9 { 10 static void Main(string[] args) 11 { 12 Student s = new Student("str"); 13 } 14 15 } 16 class Person 17 { 18 public Person() 19 { 20 Console.WriteLine("`````"); 21 } 22 23 public Person(string str) 24 { 25 Console.WriteLine("我是基类" + str); 26 } 27 28 public Person(string str, int init) 29 { 30 Console.WriteLine("我是基类" + str + init); 31 } 32 33 } 34 35 class Student : Person 36 { 37 38 public Student():this("str") 39 { 40 Console.WriteLine("```b``"); 41 } 42 43 public Student(string str):this(str,10) 44 45 { 46 Console.WriteLine("我是子类" + str); 47 } 48 49 public Student(string str, int init) 50 { 51 Console.WriteLine("我是子类" + str + init); 52 } 53 54 } 55 }
结果:
This主要作用就是可以调用其他的构造函数;