C#接口的实现

  1. using System;
  2.   
  3. interface Runner 
  4.     void run();
  5. }
  6.   
  7. interface Swimmer 
  8.     void swim();
  9. }
  10.   
  11. abstract class Animal  
  12. {   
  13.     abstract public void eat();
  14. }  
  15.   
  16. class Person : Animal , Runner, Swimmer 
  17. {
  18.     public void run() 
  19.     
  20.         Console.WriteLine("run"); 
  21.     }
  22.     public void swim()  
  23.     {
  24.         Console.WriteLine("swim"); 
  25.     }
  26.     public override void eat() 
  27.     
  28.         Console.WriteLine("eat"); 
  29.     }
  30.     public void speak()
  31.     {
  32.         Console.WriteLine("speak"); 
  33.     }
  34. }
  35.   
  36. class TestInterface
  37. {
  38.     static void m1(Runner r) 
  39.     
  40.         r.run(); 
  41.     }
  42.     static void m2(Swimmer s) 
  43.     
  44.         s.swim(); 
  45.     }
  46.     static void m3(Animal a) 
  47.     {
  48.         a.eat(); 
  49.     }
  50.     static void m4(Person p)
  51.     {
  52.         p.speak();
  53.     }
  54.   
  55.     public static void Main(string [] args)
  56.     {
  57.         Person p = new Person();
  58.         m1(p);
  59.         m2(p);
  60.         m3(p);
  61.         m4(p);
  62.         Runner a = new Person();
  63.         a.run();
  64.   
  65.     }
  66. }





















  1. using System;
  2. class InterfaceExplicitImpl
  3. {
  4.     static void Main()
  5.     {
  6.         FileViewer f = new FileViewer();
  7.         f.Test();
  8.         ( (IWindow) f ).Close();
  9.           
  10.         IWindow w = new FileViewer();
  11.         w.Close();
  12.     }
  13. }
  14.   
  15. interface IWindow
  16. {
  17.     void Close();
  18. }
  19. interface IFileHandler
  20. {
  21.     void Close();
  22. }
  23. class FileViewer : IWindow, IFileHandler
  24. {
  25.     void IWindow.Close ()
  26.     {
  27.         Console.WriteLine( "Window Closed" );
  28.     }
  29.     void IFileHandler.Close()
  30.     {
  31.         Console.WriteLine( "File Closed" );
  32.     }
  33.     public void Test()
  34.     {
  35.         ( (IWindow) this ).Close();
  36.     }
  37. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值