一、设计程序
using System;public interface IPrintable //接口定义1
{void Print();void Print2();
}public interface IPrintable2 : IPrintable //接口定义2
{void Print3();
}public class MethodAchieve : IPrintable, IPrintable2 //接口实现1
{public void Print(){Console.WriteLine("method1");}public void Print2(){Console.WriteLine("method2");}public void Print3(){Console.WriteLine("method3");}
}public class MethodAchieve2 :IPrintable //接口实现2
{public void Print(){Console.WriteLine("method1 method1");}public void Print2(){Console.WriteLine("method2 method2");}
}class Program
{static void Main(string[] args){IPrintable printer = new MethodAchieve();printer.Print(); // 输出: method1printer.Print2(); // 输出: method2IPrintable2 printers = new MethodAchieve();printers.Print3(); // 输出: method3IPrintable doubleprinter = new MethodAchieve2();doubleprinter.Print(); // 输出: method1 method1doubleprinter.Print2(); // 输出: method2 method2}
}
二、分析
1.接口实现继承接口定义,接口实现必须含有接口定义的所有方法
2.接口定义之间可以(多)继承,接口可实现多个接口
3.多态性:不同的实现,同样的方法(接口),可能有不同的结果。