接口实现多态性——》源代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace The_001_Thread
{
class Program
{
static void Main(string[] args)
{
IPersonable person = new Man();
IPersonable person1 = new Woman();
test(person);
test(person1);
Console.ReadKey();
}
static void test(IPersonable person)
{
person.say();
}
}
interface IPersonable
{
void say();
}
class Man : IPersonable
{
public void say()
{
Console.WriteLine("男人");
}
}
class Woman : IPersonable
{
public void say()
{
Console.WriteLine("女人");
}
}
}
输出结果:
从这个结果可以看得出来,通过接口实现了多态性,同时使该类多了一种能力。