1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Text;
6
7
namespace Example10_9
8
9
{
10
11
class Program
12
13
{
14
15
static void Main(string[] args)
16
17
{
18
19
//创建Circle类变量circle,并使用其作为参数创建MyClass型变量myClass
20
21
Circle circle = new Circle(35);
22
23
MyClass myClass = new MyClass(circle);
24
25
26
27
//获取返回值,并输出其面积Area属性
28
29
Circle circle1 = (Circle)myClass.MyOutput(circle);
30
31
Console.WriteLine(circle1.Area);
32
33
Console.ReadLine();
34
35
}
36
37
}
38
39
/// <summary>
40
41
/// IShape接口
42
43
/// </summary>
44
45
interface IShape
46
47
{
48
49
/// <summary>
50
51
/// Area属性
52
53
/// </summary>
54
55
int Area
56
57
{
58
59
get;
60
61
set;
62
63
}
64
65
/// <summary>
66
67
/// Caculate方法
68
69
/// </summary>
70
71
void Caculate();
72
73
}
74
75
/// <summary>
76
77
/// Circle类继承IShape
78
79
/// </summary>
80
81
class Circle:IShape
82
83
{
84
85
/// <summary>
86
87
/// area字段
88
89
/// </summary>
90
91
int area = 0;
92
93
/// <summary>
94
95
/// 构造函数
96
97
/// </summary>
98
99
/// <param name="m_Area">m_Area参数</param>
100
101
public Circle(int m_Area)
102
103
{
104
105
area = m_Area;
106
107
}
108
109
IShape 成员
154
155
}
156
157
/// <summary>
158
159
/// MyClass类
160
161
/// </summary>
162
163
class MyClass
164
165
{
166
167
/// <summary>
168
169
/// 构造函数
170
171
/// </summary>
172
173
/// <param name="m_shape">IShape型参数</param>
174
175
public MyClass(IShape m_shape)
176
177
{
178
179
m_shape.Caculate();
180
181
Console.WriteLine(m_shape.Area);
182
183
}
184
185
/// <summary>
186
187
/// MyOutput方法
188
189
/// </summary>
190
191
/// <param name="m_shape">IShape接口类型参数</param>
192
193
/// <returns>IShape接口类型返回值</returns>
194
195
public IShape MyOutput(IShape m_shape)
196
197
{
198
199
m_shape.Area = 100;
200
201
return m_shape;
202
203
}
204
205
}
206
207
}
208
209
using System;2

3
using System.Collections.Generic;4

5
using System.Text;6

7
namespace Example10_98

9
{10

11
class Program12

13
{14

15
static void Main(string[] args)16

17
{18

19
//创建Circle类变量circle,并使用其作为参数创建MyClass型变量myClass20

21
Circle circle = new Circle(35);22

23
MyClass myClass = new MyClass(circle);24

25
26

27
//获取返回值,并输出其面积Area属性28

29
Circle circle1 = (Circle)myClass.MyOutput(circle);30

31
Console.WriteLine(circle1.Area);32

33
Console.ReadLine();34

35
}36

37
}38

39
/// <summary>40

41
/// IShape接口42

43
/// </summary>44

45
interface IShape46

47
{48

49
/// <summary>50

51
/// Area属性52

53
/// </summary>54

55
int Area56

57
{58

59
get;60

61
set;62

63
}64

65
/// <summary>66

67
/// Caculate方法68

69
/// </summary>70

71
void Caculate();72

73
}74

75
/// <summary>76

77
/// Circle类继承IShape78

79
/// </summary>80

81
class Circle:IShape82

83
{84

85
/// <summary>86

87
/// area字段88

89
/// </summary>90

91
int area = 0;92

93
/// <summary>94

95
/// 构造函数96

97
/// </summary>98

99
/// <param name="m_Area">m_Area参数</param>100

101
public Circle(int m_Area)102

103
{104

105
area = m_Area;106

107
}108

109
IShape 成员154

155
}156

157
/// <summary>158

159
/// MyClass类160

161
/// </summary>162

163
class MyClass164

165
{166

167
/// <summary>168

169
/// 构造函数170

171
/// </summary>172

173
/// <param name="m_shape">IShape型参数</param>174

175
public MyClass(IShape m_shape)176

177
{178

179
m_shape.Caculate();180

181
Console.WriteLine(m_shape.Area);182

183
}184

185
/// <summary>186

187
/// MyOutput方法188

189
/// </summary>190

191
/// <param name="m_shape">IShape接口类型参数</param>192

193
/// <returns>IShape接口类型返回值</returns>194

195
public IShape MyOutput(IShape m_shape)196

197
{198

199
m_shape.Area = 100;200

201
return m_shape;202

203
}204

205
}206

207
}208

209

实例中定义了一个IShape接口,表示图形,另外定义了一个Circle类,表示圆形,并实现了IShape接口,在MyClass类中有一个MyOutput方法,该方法的返回值是IShape接口类型的.
在为MyOutput类的构造函数(参数为接口的对象)传值时,传入了继承此接口的类对象.Doesn't make sense!! 如果我的实例重写接口中方法的时候扩展很大的情况不会出现问题吗?
来源:https://www.cnblogs.com/zencorn/archive/2007/09/06/884812.html

