C# 接口


using
System; class testInterface { // class Bird { public void Run() { Console.WriteLine("鸟在奔跑!"); } } //接口 public interface IFlyable { //接口和抽象类一样,也是只能有方法的声明,不能有任何的实现 void Fly(); } //麻雀 class Sparrow : Bird, IFlyable { #region IFlyable 成员 public void Fly() { Console.WriteLine("小麻雀飞在树林中。"); } #endregion } //鹦鹉 class Parrot : Bird, IFlyable { #region IFlyable 成员 public void Fly() { Console.WriteLine("鹦鹉在小笼子里飞..."); } #endregion } //企鹅 class Penguin : Bird { } static void Main() { IFlyable fly = new Parrot(); fly.Fly(); } }

Output:

鹦鹉在小笼子里飞...

原文地址:https://www.cnblogs.com/bluestorm/p/3397668.html