code_junkie

Multiple Inheritance in C#

Nov 11th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. public interface IFirst { void FirstMethod(); }
  2. public interface ISecond { void SecondMethod(); }
  3.  
  4. public class First:IFirst
  5. {
  6. public void FirstMethod() { Console.WriteLine("Frist"); }
  7. }
  8.  
  9. public class Second:ISecond
  10. {
  11. public void SecondMethod() { Console.WriteLine("Second"); }
  12. }
  13.  
  14. public class FirstAndSecond: IFirst, ISecond
  15. {
  16. First first = new First();
  17. Second second = new Second();
  18. public void FirstMethod() { first.FirstMethod(); }
  19. public void SecondMethod() { second.SecondMethod(); }
  20. }
  21.  
  22. public class FirstAndSecond: IFirst from First, ISecond from Second
  23. { }
  24.  
  25. public interface ISteerable { SteeringWheel wheel { get; set; } }
  26.  
  27. public interface IBrakable { BrakePedal brake { get; set; } }
  28.  
  29. public class Vehicle : ISteerable, IBrakable
  30. {
  31. public SteeringWheel wheel { get; set; }
  32.  
  33. public BrakePedal brake { get; set; }
  34.  
  35. public Vehicle() { wheel = new SteeringWheel(); brake = new BrakePedal(); }
  36. }
  37.  
  38. public static class SteeringExtensions
  39. {
  40. public static void SteerLeft(this ISteerable vehicle)
  41. {
  42. vehicle.wheel.SteerLeft();
  43. }
  44. }
  45.  
  46. public static class BrakeExtensions
  47. {
  48. public static void Stop(this IBrakable vehicle)
  49. {
  50. vehicle.brake.ApplyUntilStop();
  51. }
  52. }
  53.  
  54.  
  55. public class Main
  56. {
  57. Vehicle myCar = new Vehicle();
  58.  
  59. public void main()
  60. {
  61. myCar.SteerLeft();
  62. myCar.Stop();
  63. }
  64. }
  65.  
  66. public interface IFirst {}
  67. public interface ISecond {}
  68.  
  69. public class FirstAndSecond : IFirst, ISecond
  70. {
  71. }
  72.  
  73. public static MultipleInheritenceExtensions
  74. {
  75. public static void First(this IFirst theFirst)
  76. {
  77. Console.WriteLine("First");
  78. }
  79.  
  80. public static void Second(this ISecond theSecond)
  81. {
  82. Console.WriteLine("Second");
  83. }
  84. }
  85.  
  86. public void Test()
  87. {
  88. FirstAndSecond fas = new FirstAndSecond();
  89. fas.First();
  90. fas.Second();
  91. }
  92.  
  93. using NRoles;
  94.  
  95. public interface IFirst { void FirstMethod(); }
  96. public interface ISecond { void SecondMethod(); }
  97.  
  98. public class RFirst : IFirst, Role {
  99. public void FirstMethod() { Console.WriteLine("Frist"); }
  100. }
  101.  
  102. public class RSecond : ISecond, Role {
  103. public void SecondMethod() { Console.WriteLine("Second"); }
  104. }
  105.  
  106. public class FirstAndSecond : Does<RFirst>, Does<RSecond> { }
  107.  
  108. var fas = new FirstAndSecond();
  109. fas.As<RFirst>().FirstMethod();
  110. fas.As<RSecond>().SecondMethod();
  111.  
  112. var fas = new FirstAndSecond();
  113. fas.FirstMethod();
  114. fas.SecondMethod();
Add Comment
Please, Sign In to add comment