1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace
CSharpFoundationStudy
6![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
8
* From:http://www.cnblogs.com/anytao [你必须知道的.NET] 第六回:深入浅出关键字---base和this
9
* 关键字:base 与 this
10
* base和this在C#中被归于访问关键字,顾名思义,就是用于实现继承机制的访问操作,来满足对对象成员的访问,从而为多态机制提供更加灵活的处理方式
11
*
12
* base 关键字
13
* 其用于在派生类中实现对基类公有或者受保护成员的访问,但是只局限在构造函数、实例方法和实例属性访问器中,MSDN中小结的具体功能包括:
14
* 调用基类上已被其他方法重写的方法。
15
* 指定创建派生类实例时应调用的基类构造函数。
16
* this 关键字
17
* 其用于引用类的当前实例,也包括继承而来的方法,通常可以隐藏this,MSDN中的小结功能主要包括:
18
* 限定被相似的名称隐藏的成员
19
* 将对象作为参数传递到其他方法
20
* 声明索引器
21
*/
22
public class Action
23![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
public static void ToRun(Vehicle vehicle)
25![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
Console.WriteLine("{0} is Running", vehicle.ToString());
27
}
28
}
29![](/Images/OutliningIndicators/InBlock.gif)
30
public class Vehicle
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
private string name;
33
private int speed;
34
private string[] array = new string[10];
35![](/Images/OutliningIndicators/InBlock.gif)
36
public Vehicle()
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
}
39![](/Images/OutliningIndicators/InBlock.gif)
40
public Vehicle(string name, int speed)
41![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
//限定被相似的名称隐藏的成员
43
this.name = name;
44
this.speed = speed;
45
}
46![](/Images/OutliningIndicators/InBlock.gif)
47
public virtual void ShowResult()
48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
Console.WriteLine("The top speed of {0} is {1}", name, speed);
50
}
51![](/Images/OutliningIndicators/InBlock.gif)
52
public void Run()
53![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
//传递当前实例
55
Action.ToRun(this);
56
}
57![](/Images/OutliningIndicators/InBlock.gif)
58
//声明索引器,必须为this,这样就可以像数组一样来索引对象
59
public string this[int index]
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
get
62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
return array[index];
64
}
65
set
66![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
array[index] = value;
68
}
69
}
70
}
71![](/Images/OutliningIndicators/InBlock.gif)
72
public class Car : Vehicle
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
//派生类和基类通信,以base实现,基类首先被调用
75
public Car(string name, int speed)
76
: base(name, speed)
77![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
}
79![](/Images/OutliningIndicators/InBlock.gif)
80
public Car()
81
: base("Car", 200)
82![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
}
84![](/Images/OutliningIndicators/InBlock.gif)
85
public override void ShowResult()
86![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
base.ShowResult();
88
Console.WriteLine("Override Base Function in Car");
89
}
90
}
91![](/Images/OutliningIndicators/InBlock.gif)
92
public class Audi : Car
93![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
public Audi()
95
: base("Audi", 300)
96![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
97![](/Images/OutliningIndicators/InBlock.gif)
98
public Audi(string name, int speed)
99
: base(name,speed)
100![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
}
102![](/Images/OutliningIndicators/InBlock.gif)
103
public override void ShowResult()
104![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
//由三层继承可以看出,base只能继承其直接基类成员
106
base.ShowResult();
107
//base可以访问基类的公有成员和受保护成员,私有成员是不可访问的
108
base.Run();
109
Console.WriteLine("It's audi's result.");
110
}
111
}
112![](/Images/OutliningIndicators/InBlock.gif)
113![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
114
* base常用于,在派生类对象初始化时和基类进行通信。
115
* base可以访问基类的公有成员和受保护成员,私有成员是不可访问的。
116
* this指代类对象本身,用于访问本类的所有常量、字段、属性和方法成员,而且不管访问元素是任何访问级别。因为,this仅仅局限于对象内部,对象外部是无法看到的,这就是this的基本思想。另外,静态成员不是对象的一部分,因此不能在静态方法中引用this。
117
* 在多层继承中,base可以指向的父类的方法有两种情况:一是有重载存在的情况下,base将指向直接继承的父类成员的方法,例如Audi类中的 ShowResult方法中,使用base访问的将是Car.ShowResult()方法,而不能访问Vehicle.ShowResult()方法;而是没有重载存在的情况下,base可以指向任何上级父类的公有或者受保护方法,例如Audi类中,可以使用base访问基类Vehicle.Run() 方法。
118
*/
119![](/Images/OutliningIndicators/InBlock.gif)
120
public class BaseThisTest
121![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
public static void Main()
123![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
Audi audi = new Audi("Audi", 500);
125
audi[0] = "A6";
126
audi[1] = "A8";
127![](/Images/OutliningIndicators/InBlock.gif)
128
audi.Run();
129
audi.ShowResult();
130
Console.ReadLine();
131
}
132
}
133
}
134
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/InBlock.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/InBlock.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
132
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
133
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
134
![](/Images/OutliningIndicators/None.gif)