使用接口作为返回值
1
using System;
2![](/Images/OutliningIndicators/None.gif)
3
using System.Collections.Generic;
4![](/Images/OutliningIndicators/None.gif)
5
using System.Text;
6![](/Images/OutliningIndicators/None.gif)
7
namespace Example10_9
8![](/Images/OutliningIndicators/None.gif)
9
{
10![](/Images/OutliningIndicators/InBlock.gif)
11
class Program
12![](/Images/OutliningIndicators/InBlock.gif)
13
{
14![](/Images/OutliningIndicators/InBlock.gif)
15
static void Main(string[] args)
16![](/Images/OutliningIndicators/InBlock.gif)
17
{
18![](/Images/OutliningIndicators/InBlock.gif)
19
//创建Circle类变量circle,并使用其作为参数创建MyClass型变量myClass
20![](/Images/OutliningIndicators/InBlock.gif)
21
Circle circle = new Circle(35);
22![](/Images/OutliningIndicators/InBlock.gif)
23
MyClass myClass = new MyClass(circle);
24![](/Images/OutliningIndicators/InBlock.gif)
25
26![](/Images/OutliningIndicators/InBlock.gif)
27
//获取返回值,并输出其面积Area属性
28![](/Images/OutliningIndicators/InBlock.gif)
29
Circle circle1 = (Circle)myClass.MyOutput(circle);
30![](/Images/OutliningIndicators/InBlock.gif)
31
Console.WriteLine(circle1.Area);
32![](/Images/OutliningIndicators/InBlock.gif)
33
Console.ReadLine();
34![](/Images/OutliningIndicators/InBlock.gif)
35
}
36![](/Images/OutliningIndicators/InBlock.gif)
37
}
38![](/Images/OutliningIndicators/InBlock.gif)
39
/// <summary>
40![](/Images/OutliningIndicators/InBlock.gif)
41
/// IShape接口
42![](/Images/OutliningIndicators/InBlock.gif)
43
/// </summary>
44![](/Images/OutliningIndicators/InBlock.gif)
45
interface IShape
46![](/Images/OutliningIndicators/InBlock.gif)
47
{
48![](/Images/OutliningIndicators/InBlock.gif)
49
/// <summary>
50![](/Images/OutliningIndicators/InBlock.gif)
51
/// Area属性
52![](/Images/OutliningIndicators/InBlock.gif)
53
/// </summary>
54![](/Images/OutliningIndicators/InBlock.gif)
55
int Area
56![](/Images/OutliningIndicators/InBlock.gif)
57
{
58![](/Images/OutliningIndicators/InBlock.gif)
59
get;
60![](/Images/OutliningIndicators/InBlock.gif)
61
set;
62![](/Images/OutliningIndicators/InBlock.gif)
63
}
64![](/Images/OutliningIndicators/InBlock.gif)
65
/// <summary>
66![](/Images/OutliningIndicators/InBlock.gif)
67
/// Caculate方法
68![](/Images/OutliningIndicators/InBlock.gif)
69
/// </summary>
70![](/Images/OutliningIndicators/InBlock.gif)
71
void Caculate();
72![](/Images/OutliningIndicators/InBlock.gif)
73
}
74![](/Images/OutliningIndicators/InBlock.gif)
75
/// <summary>
76![](/Images/OutliningIndicators/InBlock.gif)
77
/// Circle类继承IShape
78![](/Images/OutliningIndicators/InBlock.gif)
79
/// </summary>
80![](/Images/OutliningIndicators/InBlock.gif)
81
class Circle:IShape
82![](/Images/OutliningIndicators/InBlock.gif)
83
{
84![](/Images/OutliningIndicators/InBlock.gif)
85
/// <summary>
86![](/Images/OutliningIndicators/InBlock.gif)
87
/// area字段
88![](/Images/OutliningIndicators/InBlock.gif)
89
/// </summary>
90![](/Images/OutliningIndicators/InBlock.gif)
91
int area = 0;
92![](/Images/OutliningIndicators/InBlock.gif)
93
/// <summary>
94![](/Images/OutliningIndicators/InBlock.gif)
95
/// 构造函数
96![](/Images/OutliningIndicators/InBlock.gif)
97
/// </summary>
98![](/Images/OutliningIndicators/InBlock.gif)
99
/// <param name="m_Area">m_Area参数</param>
100![](/Images/OutliningIndicators/InBlock.gif)
101
public Circle(int m_Area)
102![](/Images/OutliningIndicators/InBlock.gif)
103
{
104![](/Images/OutliningIndicators/InBlock.gif)
105
area = m_Area;
106![](/Images/OutliningIndicators/InBlock.gif)
107
}
108![](/Images/OutliningIndicators/InBlock.gif)
109
IShape 成员
154![](/Images/OutliningIndicators/InBlock.gif)
155
}
156![](/Images/OutliningIndicators/InBlock.gif)
157
/// <summary>
158![](/Images/OutliningIndicators/InBlock.gif)
159
/// MyClass类
160![](/Images/OutliningIndicators/InBlock.gif)
161
/// </summary>
162![](/Images/OutliningIndicators/InBlock.gif)
163
class MyClass
164![](/Images/OutliningIndicators/InBlock.gif)
165
{
166![](/Images/OutliningIndicators/InBlock.gif)
167
/// <summary>
168![](/Images/OutliningIndicators/InBlock.gif)
169
/// 构造函数
170![](/Images/OutliningIndicators/InBlock.gif)
171
/// </summary>
172![](/Images/OutliningIndicators/InBlock.gif)
173
/// <param name="m_shape">IShape型参数</param>
174![](/Images/OutliningIndicators/InBlock.gif)
175
public MyClass(IShape m_shape)
176![](/Images/OutliningIndicators/InBlock.gif)
177
{
178![](/Images/OutliningIndicators/InBlock.gif)
179
m_shape.Caculate();
180![](/Images/OutliningIndicators/InBlock.gif)
181
Console.WriteLine(m_shape.Area);
182![](/Images/OutliningIndicators/InBlock.gif)
183
}
184![](/Images/OutliningIndicators/InBlock.gif)
185
/// <summary>
186![](/Images/OutliningIndicators/InBlock.gif)
187
/// MyOutput方法
188![](/Images/OutliningIndicators/InBlock.gif)
189
/// </summary>
190![](/Images/OutliningIndicators/InBlock.gif)
191
/// <param name="m_shape">IShape接口类型参数</param>
192![](/Images/OutliningIndicators/InBlock.gif)
193
/// <returns>IShape接口类型返回值</returns>
194![](/Images/OutliningIndicators/InBlock.gif)
195
public IShape MyOutput(IShape m_shape)
196![](/Images/OutliningIndicators/InBlock.gif)
197
{
198![](/Images/OutliningIndicators/InBlock.gif)
199
m_shape.Area = 100;
200![](/Images/OutliningIndicators/InBlock.gif)
201
return m_shape;
202![](/Images/OutliningIndicators/InBlock.gif)
203
}
204![](/Images/OutliningIndicators/InBlock.gif)
205
}
206![](/Images/OutliningIndicators/InBlock.gif)
207
}
208![](/Images/OutliningIndicators/None.gif)
209![](/Images/OutliningIndicators/None.gif)
![](/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/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
154
![](/Images/OutliningIndicators/InBlock.gif)
155
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
156
![](/Images/OutliningIndicators/InBlock.gif)
157
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
158
![](/Images/OutliningIndicators/InBlock.gif)
159
![](/Images/OutliningIndicators/InBlock.gif)
160
![](/Images/OutliningIndicators/InBlock.gif)
161
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
162
![](/Images/OutliningIndicators/InBlock.gif)
163
![](/Images/OutliningIndicators/InBlock.gif)
164
![](/Images/OutliningIndicators/InBlock.gif)
165
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
166
![](/Images/OutliningIndicators/InBlock.gif)
167
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
168
![](/Images/OutliningIndicators/InBlock.gif)
169
![](/Images/OutliningIndicators/InBlock.gif)
170
![](/Images/OutliningIndicators/InBlock.gif)
171
![](/Images/OutliningIndicators/InBlock.gif)
172
![](/Images/OutliningIndicators/InBlock.gif)
173
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
174
![](/Images/OutliningIndicators/InBlock.gif)
175
![](/Images/OutliningIndicators/InBlock.gif)
176
![](/Images/OutliningIndicators/InBlock.gif)
177
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
178
![](/Images/OutliningIndicators/InBlock.gif)
179
![](/Images/OutliningIndicators/InBlock.gif)
180
![](/Images/OutliningIndicators/InBlock.gif)
181
![](/Images/OutliningIndicators/InBlock.gif)
182
![](/Images/OutliningIndicators/InBlock.gif)
183
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
184
![](/Images/OutliningIndicators/InBlock.gif)
185
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
186
![](/Images/OutliningIndicators/InBlock.gif)
187
![](/Images/OutliningIndicators/InBlock.gif)
188
![](/Images/OutliningIndicators/InBlock.gif)
189
![](/Images/OutliningIndicators/InBlock.gif)
190
![](/Images/OutliningIndicators/InBlock.gif)
191
![](/Images/OutliningIndicators/InBlock.gif)
192
![](/Images/OutliningIndicators/InBlock.gif)
193
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
194
![](/Images/OutliningIndicators/InBlock.gif)
195
![](/Images/OutliningIndicators/InBlock.gif)
196
![](/Images/OutliningIndicators/InBlock.gif)
197
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
198
![](/Images/OutliningIndicators/InBlock.gif)
199
![](/Images/OutliningIndicators/InBlock.gif)
200
![](/Images/OutliningIndicators/InBlock.gif)
201
![](/Images/OutliningIndicators/InBlock.gif)
202
![](/Images/OutliningIndicators/InBlock.gif)
203
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
204
![](/Images/OutliningIndicators/InBlock.gif)
205
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
206
![](/Images/OutliningIndicators/InBlock.gif)
207
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
208
![](/Images/OutliningIndicators/None.gif)
209
![](/Images/OutliningIndicators/None.gif)
实例中定义了一个IShape接口,表示图形,另外定义了一个Circle类,表示圆形,并实现了IShape接口,在MyClass类中有一个MyOutput方法,该方法的返回值是IShape接口类型的.
在为MyOutput类的构造函数(参数为接口的对象)传值时,传入了继承此接口的类对象.Doesn't make sense!! 如果我的实例重写接口中方法的时候扩展很大的情况不会出现问题吗?
- Make people around you successful is the biggest contribution to ourselves. -