案例來自: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!");
}
}
}