[C#] C#接口interface使用范例 →→→→→进入此内容的聊天室

来自 , 2020-12-18, 写在 C#, 查看 131 次.
URL http://www.code666.cn/view/04255181
  1. using System;
  2. //example of interfaces
  3. public class Animals
  4. {
  5. //simple interface
  6. interface IAnimal {
  7.   void Breathes();
  8. }
  9. //interfaces can inherent from other interfaces
  10. interface IMammal : IAnimal {
  11.   int HairLength();
  12. }
  13. //interfaces can implement other interfaces which implemented interfaces
  14. interface IMarsupial : IMammal {
  15.   int PouchSize();
  16. }
  17. //interfaces can implement many other interfaces
  18. interface IGonerMammal : IMammal, IExtinct {
  19. }
  20. interface IExtinct {
  21.   int HowLongExtinct();
  22. }
  23. //classes can implement multiple interfaces
  24. public class TasmanianTiger : IGonerMammal, IMarsupial {
  25. public int PouchSize() { return 2; }
  26. public int HowLongExtinct() { return 28; }
  27. public int HairLength() { return 4; }
  28. public void Breathes() { }
  29. }
  30.  
  31.     public static void Main(string[] args) {
  32.         Console.Write("The Tasmanian Tiger has been extinct for {0} years", new TasmanianTiger().HowLongExtinct());
  33.     }
  34. }
  35.  
  36. //csharp/4865

回复 "C#接口interface使用范例"

这儿你可以回复上面这条便签

captcha