首页 > 代码库 > 接口练习代码

接口练习代码

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接口

      
        public interface 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,要不 检查代码认为没有实现该接口

接口练习代码