日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#接口(Interface)理解

發布時間:2025/3/15 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#接口(Interface)理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • C#接口(Interface)理解

C#接口(Interface)理解

接口定義了所有類繼承接口時應遵循的語法合同。

接口定義了語法合同 “是什么” 部分,派生類定義了語法合同 “怎么做” 部分。

接口只包含了成員的聲明。成員的定義是派生類的責任。

接口聲明可以包含以下成員的聲明(沒有任何實現的簽名):

  • 方法
  • 屬性
  • 索引器
  • 事件

示例:
下例演示了接口實現。 在此示例中,接口包含屬性聲明,類包含實現。 實現 IPoint 的類的任何實例都具有整數屬性 x 和 y。

namespace interfaceDemo {interface IPoint{// Property signatures:int X{get;set;}int Y{get;set;}// method signatures:double Distance{get;}void print();}class Point : IPoint {// Constructor:public Point(int x, int y){X = x;Y = y;}// Property implementation:public int X { get; set; }public int Y { get; set; }// Property implementationpublic double Distance{get { return Math.Sqrt(X * X + Y * Y); }set { } }// Method implementation.void IPoint.print(){Console.WriteLine("This is a Point!");} }class Program {static void PrintPoint(IPoint p){Console.WriteLine("x={0}, y={1},dis ={2}", p.X, p.Y,p.Distance);}static void Main(string[] args){IPoint p = new Point(4, 3);Console.Write("My Point: ");PrintPoint(p);p.print();Console.ReadLine();}} }

以上的示例中,顯示定義了接口IPoint,包含了屬性和方法的成員;Point繼承了該接口,而且實現了接口的成員,最后在main函數創建Point實例.

總結

以上是生活随笔為你收集整理的C#接口(Interface)理解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。