星空网 > 软件开发 > ASP.net

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

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

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 Width14 {15 get;16 set;17 }18 /// <summary>19 /// 宽20 /// </summary>21 int Height22 {23 get;24 set;25 }26 /// <summary>27 /// 计算矩形面积28 /// </summary>29 int Area();30 }31 class Program : ImyInterface//继承自接口32 {33 private int width = 0;34 private int height = 0;35 /// <summary>36 /// 长37 /// </summary>38 public int Width39 {40 get41 {42 return width;43 }44 set45 {46 width = value;47 }48 }49 /// <summary>50 /// 宽51 /// </summary>52 public int Height53 {54 get55 {56 return height;57 }58 set59 {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;//使用派生类对象实例化接口ImyInterface74 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 Width14     {15       get;16       set;17     }18     /// <summary>19     /// 宽20     /// </summary>21     int Height22     {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 Width39     {40       get41       {42         return width;43       }44       set45       {46         width = value;47       }48     }49     /// <summary>50     /// 宽51     /// </summary>52     public int Height53     {54       get55       {56         return height;57       }58       set59       {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;//使用派生类对象实例化接口ImyInterface74       imyinterface.Width = 5;//为派生类中的Width属性赋值75       imyinterface.Height = 3;//为派生类中的Height属性赋值76       Console.WriteLine("矩形的面积为:" + imyinterface.Area());77     }78   }79 }

 


 




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

关键词:接口

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Snapdeal官网网址,Snapdeal平台是哪国的?:https://www.kjdsnews.com/a/775475.html
Lazada越南站发布电商行业报告,Shopee调整三站点运:https://www.kjdsnews.com/a/775476.html
你选的产品在亚马逊上是否需要审核?两种方法告诉你:https://www.kjdsnews.com/a/775477.html
记得收藏!eBay常见问题详解(五):https://www.kjdsnews.com/a/775478.html
柬埔寨推迟电商增值税,SHEIN重启赴美IPO计划:https://www.kjdsnews.com/a/775479.html
解析三大DTC品牌的流量来源,竟然“大同小异”?:https://www.kjdsnews.com/a/775480.html
浏阳市里面哪里好玩的地方 浏阳市内有什么好玩的地方:https://www.vstour.cn/a/404233.html
你心目中的十大旅游景点有哪些?:https://www.vstour.cn/a/404234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流