C# 继承中的New Base This

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主要作用就是可以调用其他的构造函数;

 

转载于:https://www.cnblogs.com/pyffcwj/articles/2936859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值