C#: 接口(关键字:interface)
1.代码(入门举例)
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-------------------------------------");
IIntroduce iSE = new SoftwareEngineer();
iSE.SayHi();
iSE.DescribeMyself();
iSE.SayGoodbye(); Console.WriteLine("-------------------------------------");
IIntroduce iTc = new Teacher();
iTc.SayHi();
iTc.DescribeMyself();
iTc.SayGoodbye(); Console.ReadKey();
}
} interface IIntroduce
{
void SayHi();
void DescribeMyself();
void SayGoodbye(); } class SoftwareEngineer : IIntroduce
{
public void DescribeMyself()
{
Console.WriteLine("I'm a software engineer !");
} public void SayGoodbye()
{
Console.WriteLine("Goodbye !");
} public void SayHi()
{
Console.WriteLine("Hi !");
} } class Teacher : IIntroduce
{
public void DescribeMyself()
{
Console.WriteLine("I'm a Teacher !");
}
public void SayGoodbye()
{
Console.WriteLine("Goodbye !");
}
public void SayHi()
{
Console.WriteLine("Hi !");
}
}
2. 运行结果: