接口里不能包括字段,构造函数,析构函数,静态成员或常量等,否则会导致错误...

定义一个借口,接口封装了矩形的长和宽,而且还包含一个自定义的方法以计算矩形的面积。然后定义一个类,继承自该接口,在该类中实现接口中自定义方法。

1
using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Test06 7 { 8 interface ImyInterface 9 { 10 /// <summary> 11 ///12 /// </summary> 13 int Width 14 { 15 get; 16 set; 17 } 18 /// <summary> 19 ///20 /// </summary> 21 int Height 22 { 23 get; 24 set; 25 } 26 /// <summary> 27 /// 计算矩形面积 28 /// </summary> 29 int Area();  int Area(int Width, int Height); 30 } 31 class Program : ImyInterface//继承自接口 32 { 33 private int width = 0; 34 private int height = 0; 35 /// <summary> 36 ///37 /// </summary> 38 public int Width 39 { 40 get 41 { 42 return width; 43 } 44 set 45 { 46 width = value; 47 } 48 } 49 /// <summary> 50 ///51 /// </summary> 52 public int Height 53 { 54 get 55 { 56 return height; 57 } 58 set 59 { 60 height = value; 61 } 62 } 63 /// <summary> 64 /// 计算矩形面积 65 /// </summary> 66 public int Area(int Width,int Height) 67 { 68 return Width * Height; 69 } 70 static void Main(string[] args) 71 { 72 Program program = new Program();//实例化Program类对象 73 ImyInterface imyinterface = program;//使用派生类对象实例化接口ImyInterface 74 imyinterface.Width = 5;//为派生类中的Width属性赋值 75 imyinterface.Height = 3;//为派生类中的Height属性赋值 76 Console.WriteLine("矩形的面积为:" + imyinterface.Area(3,5)); 77 } 78 } 79 }

下面是正确的:
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Test06
 7 {
 8     interface ImyInterface
 9     {
10         /// <summary>
11         ///12         /// </summary>
13         int Width
14         {
15             get;
16             set;
17         }
18         /// <summary>
19         ///20         /// </summary>
21         int Height
22         {
23             get;
24             set;
25         }
26         /// <summary>
27         /// 计算矩形面积
28         /// </summary>
29         int Area();
30     }
31     class Program : ImyInterface//继承自接口
32     {
33         int width = 0;
34         int height = 0;
35         /// <summary>
36         ///37         /// </summary>
38         public int Width
39         {
40             get
41             {
42                 return width;
43             }
44             set
45             {
46                 width = value;
47             }
48         }
49         /// <summary>
50         ///51         /// </summary>
52         public int Height
53         {
54             get
55             {
56                 return height;
57             }
58             set
59             {
60                 height = value;
61             }
62         }
63         /// <summary>
64         /// 计算矩形面积
65         /// </summary>
66         public int Area()
67         {
68             return Width * Height;
69         }
70         static void Main(string[] args)
71         {
72             Program program = new Program();//实例化Program类对象
73             ImyInterface imyinterface = program;//使用派生类对象实例化接口ImyInterface
74             imyinterface.Width = 5;//为派生类中的Width属性赋值
75             imyinterface.Height = 3;//为派生类中的Height属性赋值
76             Console.WriteLine("矩形的面积为:" + imyinterface.Area());
77         }
78     }
79 }

 本题的关键出错点是:在接口里声明方法的时候的形式要与接口的实现里面方法的定义形式保持一致,否则会出现错误:“

错误 1 “Area”方法没有任何重载采用“2”个参数”

 

转载于:https://www.cnblogs.com/liuyaozhi/p/4942234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值