c#中使用並實現接口

案例來自:unity與c++網絡開發實踐

using System;

namespace LearnInterface
{
    public interface Iworker { void work(string a); }
    class AIReal1 : Iworker
    {
      public  void work(string a)
        {
            Console.WriteLine("我的名字是仿真人1"+"我的工作是"+a);
        }
    }
    class AIReal2 : Iworker
    {
        public void work(string a)
        {
            Console.WriteLine("我的名字是仿真人2" + "我的工作是" + a);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //通過接口實例化ai1
            Iworker ai1 = new AIReal1();
            //通過接口實例化ai2
            Iworker ai2 = new AIReal2();
           // 通過接口實現ai1
            ai1.work("美工");
           // 通過接口實現ai2
            ai2.work("編程");
            Console.WriteLine("Hello World!");
        }
    }
    
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章