code_junkie

Should C# include multiple inheritance?

Nov 11th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. class foo : bar, baz
  2.  
  3. class foo : Ibarrable, Ibazzable
  4. {
  5. ...
  6. public Bar TheBar{ set }
  7. public Baz TheBaz{ set }
  8.  
  9. public void BarFunction()
  10. {
  11. TheBar.doSomething();
  12. }
  13. public Thing BazFunction( object param )
  14. {
  15. return TheBaz.doSomethingComplex(param);
  16. }
  17. }
  18.  
  19. abstract class Gun
  20. { public void Shoot(object target) {}
  21. public void Shoot() {}
  22.  
  23. public abstract void Reload();
  24.  
  25. public void Cock() { Console.Write("Gun cocked."); }
  26. }
  27.  
  28. class Camera
  29. { public void Shoot(object subject) {}
  30. public virtual void Reload() {}
  31. public virtual void Focus() {}
  32. }
  33.  
  34. //this is great for taking pictures of targets!
  35. class PhotoPistol : Gun, Camera
  36. { public override void Reload() { Console.Write("Gun reloaded."); }
  37. public override void Camera.Reload()
  38. { Console.Write("Camera reloaded.");
  39. }
  40. public override void Focus() {}
  41. }
  42.  
  43.  
  44. var pp = new PhotoPistol();
  45. Gun gun = pp;
  46. Camera camera = pp;
  47.  
  48. pp.Shoot(); //Gun.Shoot()
  49. pp.Reload(); //writes "Gun reloaded"
  50. camera.Reload(); //writes "Camera reloaded"
  51. pp.Cock(); //writes "Gun cocked."
  52. camera.Cock(); //error: Camera.Cock() not found
  53. ((PhotoPistol) camera).Cock(); //writes "Gun cocked."
  54. camera.Shoot(); //error: Camera.Shoot() not found
  55. ((PhotoPistol) camera).Shoot();//Gun.Shoot()
  56. pp.Shoot(target); //Gun.Shoot(target)
  57. camera.Shoot(target); //Camera.Shoot(target)
  58.  
  59. public class PhotoPistol : Gun /* ,Camera */
  60. { PhotoPistolCamera camera;
  61. public PhotoPistol() {camera = new PhotoPistolCamera();}
  62. public void Focus() { camera.Focus(); }
  63. class PhotoPistolCamera : Camera { public override Focus() { }}
  64. public static Camera implicit operator(PhotoPistol p)
  65. { return p.camera;
  66. }
  67. }
  68.  
  69. /// This trait declares default methods of IList<T>
  70. public trait DefaultListMethods<T> : IList<T>
  71. {
  72. // Methods without bodies must be implemented by another
  73. // trait or by the class
  74. public void Insert(int index, T item);
  75. public void RemoveAt(int index);
  76. public T this[int index] { get; set; }
  77. public int Count { get; }
  78.  
  79. public int IndexOf(T item)
  80. {
  81. EqualityComparer<T> comparer = EqualityComparer<T>.Default;
  82. for (int i = 0; i < Count; i++)
  83. if (comparer.Equals(this[i], item))
  84. return i;
  85. return -1;
  86. }
  87. public void Add(T item)
  88. {
  89. Insert(Count, item);
  90. }
  91. public void Clear()
  92. { // Note: the class would be allowed to override the trait
  93. // with a better implementation, or select an
  94. // implementation from a different trait.
  95. for (int i = Count - 1; i >= 0; i--)
  96. RemoveAt(i);
  97. }
  98. public bool Contains(T item)
  99. {
  100. return IndexOf(item) != -1;
  101. }
  102. public void CopyTo(T[] array, int arrayIndex)
  103. {
  104. foreach (T item in this)
  105. array[arrayIndex++] = item;
  106. }
  107. public bool IsReadOnly
  108. {
  109. get { return false; }
  110. }
  111. public bool Remove(T item)
  112. {
  113. int i = IndexOf(item);
  114. if (i == -1)
  115. return false;
  116. RemoveAt(i);
  117. return true;
  118. }
  119. System.Collections.IEnumerator
  120. System.Collections.IEnumerable.GetEnumerator()
  121. {
  122. return GetEnumerator();
  123. }
  124. IEnumerator<T> GetEnumerator()
  125. {
  126. for (int i = 0; i < Count; i++)
  127. yield return this[i];
  128. }
  129. }
  130.  
  131. class MyList<T> : MyBaseClass, DefaultListMethods<T>
  132. {
  133. public void Insert(int index, T item) { ... }
  134. public void RemoveAt(int index) { ... }
  135. public T this[int index] {
  136. get { ... }
  137. set { ... }
  138. }
  139. public int Count {
  140. get { ... }
  141. }
  142. }
  143.  
  144. interface I
  145. {
  146. void F();
  147. void G();
  148. }
  149.  
  150.  
  151. class DefaultI : I
  152. {
  153. void F() { ... }
  154. void G() { ... }
  155. }
  156.  
  157. class C : I = DefaultI
  158. {
  159. public void F() { ... } // implements I.F
  160. }
Add Comment
Please, Sign In to add comment