1 namespace Test 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //作业:橡皮rubber鸭子、木wood鸭子、真实的鸭子realduck。 8 //三个鸭子都会游泳,而橡皮鸭子和真实的鸭子都会叫, 9 //只是叫声不一样,橡皮鸭子“唧唧”叫,真实地鸭子“嘎嘎”叫,木鸭子不会叫. 10 11 Duck dk = new RealDuck(); 12 dk.Swim(); 13 IBark ib = new RealDuck(); 14 ib.Bark(); 15 Console.ReadKey(); 16 } 17 } 18 //为抽象类 19 public abstract class Duck 20 { 21 public abstract void Swim(); 22 } 23 //接口 24 public interface IBark 25 { 26 void Bark(); 27 } 28 29 public class RealDuck : Duck, IBark 30 { 31 public void Bark() 32 { 33 Console.WriteLine("真的鸭子嘎嘎叫"); 34 } 35 36 public override void Swim() 37 { 38 Console.WriteLine("真的鸭子会游泳"); 39 } 40 } 41 42 public class MuDuck : Duck 43 { 44 public override void Swim() 45 { 46 Console.WriteLine("木头鸭子也会游泳"); 47 } 48 } 49 50 public class XPDuck : Duck, IBark 51 { 52 public void Bark() 53 { 54 Console.WriteLine("橡皮鸭子唧唧叫"); 55 } 56 57 public override void Swim() 58 { 59 Console.WriteLine("橡皮鸭子也会游泳"); 60 } 61 } 62 }
原文地址:https://www.cnblogs.com/qicao/p/8934110.html
时间: 2024-10-14 18:16:09