1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
CSharpFoundationStudy
6
{
7
/**//*
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
{
24
public static void ToRun(Vehicle vehicle)
25
{
26
Console.WriteLine("{0} is Running", vehicle.ToString());
27
}
28
}
29
30
public class Vehicle
31
{
32
private string name;
33
private int speed;
34
private string[] array = new string[10];
35
36
public Vehicle()
37
{
38
}
39
40
public Vehicle(string name, int speed)
41
{
42
//限定被相似的名称隐藏的成员
43
this.name = name;
44
this.speed = speed;
45
}
46
47
public virtual void ShowResult()
48
{
49
Console.WriteLine("The top speed of {0} is {1}", name, speed);
50
}
51
52
public void Run()
53
{
54
//传递当前实例
55
Action.ToRun(this);
56
}
57
58
//声明索引器,必须为this,这样就可以像数组一样来索引对象
59
public string this[int index]
60
{
61
get
62
{
63
return array[index];
64
}
65
set
66
{
67
array[index] = value;
68
}
69
}
70
}
71
72
public class Car : Vehicle
73
{
74
//派生类和基类通信,以base实现,基类首先被调用
75
public Car(string name, int speed)
76
: base(name, speed)
77
{
78
}
79
80
public Car()
81
: base("Car", 200)
82
{
83
}
84
85
public override void ShowResult()
86
{
87
base.ShowResult();
88
Console.WriteLine("Override Base Function in Car");
89
}
90
}
91
92
public class Audi : Car
93
{
94
public Audi()
95
: base("Audi", 300)
96
{ }
97
98
public Audi(string name, int speed)
99
: base(name,speed)
100
{
101
}
102
103
public override void ShowResult()
104
{
105
//由三层继承可以看出,base只能继承其直接基类成员
106
base.ShowResult();
107
//base可以访问基类的公有成员和受保护成员,私有成员是不可访问的
108
base.Run();
109
Console.WriteLine("It's audi's result.");
110
}
111
}
112
113
/**//*
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
120
public class BaseThisTest
121
{
122
public static void Main()
123
{
124
Audi audi = new Audi("Audi", 500);
125
audi[0] = "A6";
126
audi[1] = "A8";
127
128
audi.Run();
129
audi.ShowResult();
130
Console.ReadLine();
131
}
132
}
133
}
134

2

3

4

5

6



7


8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23



24

25



26

27

28

29

30

31



32

33

34

35

36

37



38

39

40

41



42

43

44

45

46

47

48



49

50

51

52

53



54

55

56

57

58

59

60



61

62



63

64

65

66



67

68

69

70

71

72

73



74

75

76

77



78

79

80

81

82



83

84

85

86



87

88

89

90

91

92

93



94

95

96



97

98

99

100



101

102

103

104



105

106

107

108

109

110

111

112

113


114

115

116

117

118

119

120

121



122

123



124

125

126

127

128

129

130

131

132

133

134
