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

3
 using System.Collections.Generic;
using System.Collections.Generic;4

5
 using System.Text;
using System.Text;6

7
 namespace Example10_9
namespace Example10_98

9
 {
{10

11
 class Program
    class Program12

13
 {
    {14

15
 static void Main(string[] args)
        static void Main(string[] args)16

17
 {
        {18

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

21
 Circle circle = new Circle(35);
            Circle circle = new Circle(35);22

23
 MyClass myClass = new MyClass(circle);
            MyClass myClass = new MyClass(circle);24

25
 
            26

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

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

31
 Console.WriteLine(circle1.Area);
            Console.WriteLine(circle1.Area);32

33
 Console.ReadLine();
            Console.ReadLine();34

35
 }
        }36

37
 }
    }38

39
 /// <summary>
    /// <summary>40

41
 /// IShape接口
    /// IShape接口42

43
 /// </summary>
    /// </summary>44

45
 interface IShape
    interface IShape46

47
 {
    {48

49
 /// <summary>
        /// <summary>50

51
 /// Area属性
        /// Area属性52

53
 /// </summary>
        /// </summary>54

55
 int Area
        int Area56

57
 {
        {58

59
 get;
            get;60

61
 set;
            set;62

63
 }
        }64

65
 /// <summary>
        /// <summary>66

67
 /// Caculate方法
        /// Caculate方法68

69
 /// </summary>
        /// </summary>70

71
 void Caculate();
        void Caculate();72

73
 }
    }74

75
 /// <summary>
    /// <summary>76

77
 /// Circle类继承IShape
    /// Circle类继承IShape78

79
 /// </summary>
    /// </summary>80

81
 class Circle:IShape
    class Circle:IShape82

83
 {
    {84

85
 /// <summary>
        /// <summary>86

87
 /// area字段
        /// area字段88

89
 /// </summary>
        /// </summary>90

91
 int area = 0;
        int area = 0;92

93
 /// <summary>
        /// <summary>94

95
 /// 构造函数
        /// 构造函数96

97
 /// </summary>
        /// </summary>98

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

101
 public Circle(int m_Area)
        public Circle(int m_Area)102

103
 {
        {104

105
 area = m_Area;
            area = m_Area;106

107
 }
        }108

109
 IShape 成员
        IShape 成员154

155
 }
    }156

157
 /// <summary>
    /// <summary>158

159
 /// MyClass类
    /// MyClass类160

161
 /// </summary>
    /// </summary>162

163
 class MyClass
    class MyClass164

165
 {
    {166

167
 /// <summary>
        /// <summary>168

169
 /// 构造函数
        /// 构造函数170

171
 /// </summary>
        /// </summary>172

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

175
 public MyClass(IShape m_shape)
        public MyClass(IShape m_shape)176

177
 {
        {178

179
 m_shape.Caculate();
            m_shape.Caculate();180

181
 Console.WriteLine(m_shape.Area);
            Console.WriteLine(m_shape.Area);182

183
 }
        }184

185
 /// <summary>
        /// <summary>186

187
 /// MyOutput方法
        /// MyOutput方法188

189
 /// </summary>
        /// </summary>190

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

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

195
 public IShape MyOutput(IShape m_shape)
        public IShape MyOutput(IShape m_shape)196

197
 {
        {198

199
 m_shape.Area = 100;
            m_shape.Area = 100;200

201
 return m_shape;
            return m_shape;202

203
 }
        }204

205
 }
    }206

207
 }
}208

209

实例中定义了一个IShape接口,表示图形,另外定义了一个Circle类,表示圆形,并实现了IShape接口,在MyClass类中有一个MyOutput方法,该方法的返回值是IShape接口类型的.
在为MyOutput类的构造函数(参数为接口的对象)传值时,传入了继承此接口的类对象.Doesn't make sense!! 如果我的实例重写接口中方法的时候扩展很大的情况不会出现问题吗?
- Make people around you successful is the biggest contribution to ourselves. -
 
                    
                 


 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号