使用接口作为返回值

  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 成员#region IShape 成员
110
111        /**//// <summary>
112
113        /// Area属性
114
115        /// </summary>

116
117        public int Area
118
119        {
120
121            get
122
123            {
124
125                return area ;
126
127            }

128
129            set
130
131            {
132
133                area = value;
134
135            }

136
137        }

138
139        /**//// <summary>
140
141        /// Caculate方法
142
143        /// </summary>

144
145        public void Caculate()
146
147        {
148
149            Console.WriteLine("计算面积!");
150
151        }

152
153        #endregion

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

 

实例中定义了一个IShape接口,表示图形,另外定义了一个Circle类,表示圆形,并实现了IShape接口,在MyClass类中有一个MyOutput方法,该方法的返回值是IShape接口类型的.

在为MyOutput类的构造函数(参数为接口的对象)传值时,传入了继承此接口的类对象.Doesn't make sense!! 如果我的实例重写接口中方法的时候扩展很大的情况不会出现问题吗?

转载于:https://www.cnblogs.com/zencorn/archive/2007/09/06/884812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值