接口練習代碼
阿新 • • 發佈:2017-06-25
stat plan oid 索引 span ret write return ane
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("================接口練習============================"); plane pl= new plane(); pl[0] = "戰鬥機"; Console.WriteLine(pl[0]); Console.WriteLine(pl.Name); pl.fly(8); pl.Speed = 200; Console.WriteLine(pl.Speed); Console.ReadKey(); } //接口 #region flyable接口 publicinterface flyable { void fly(int x); string this[int x] { get; set; } int Speed { get; set; } } #endregion #region Plane類 class plane :flyable {int speed; string name; public void fly (int x) { Console.WriteLine("飛行速度為:"+x); } public string this[int x ] { set { Name = value; } get { return Name+"的序號為:"+x; } } public int Speed { get { return speed; } set { speed = value; } } public string Name { get { return name; } set { name = value; } } } #endregion }
註意 :索引器前面要加 public,要不 檢查代碼認為沒有實現該接口
接口練習代碼