Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class foo : bar, baz
- class foo : Ibarrable, Ibazzable
- {
- ...
- public Bar TheBar{ set }
- public Baz TheBaz{ set }
- public void BarFunction()
- {
- TheBar.doSomething();
- }
- public Thing BazFunction( object param )
- {
- return TheBaz.doSomethingComplex(param);
- }
- }
- abstract class Gun
- { public void Shoot(object target) {}
- public void Shoot() {}
- public abstract void Reload();
- public void Cock() { Console.Write("Gun cocked."); }
- }
- class Camera
- { public void Shoot(object subject) {}
- public virtual void Reload() {}
- public virtual void Focus() {}
- }
- //this is great for taking pictures of targets!
- class PhotoPistol : Gun, Camera
- { public override void Reload() { Console.Write("Gun reloaded."); }
- public override void Camera.Reload()
- { Console.Write("Camera reloaded.");
- }
- public override void Focus() {}
- }
- var pp = new PhotoPistol();
- Gun gun = pp;
- Camera camera = pp;
- pp.Shoot(); //Gun.Shoot()
- pp.Reload(); //writes "Gun reloaded"
- camera.Reload(); //writes "Camera reloaded"
- pp.Cock(); //writes "Gun cocked."
- camera.Cock(); //error: Camera.Cock() not found
- ((PhotoPistol) camera).Cock(); //writes "Gun cocked."
- camera.Shoot(); //error: Camera.Shoot() not found
- ((PhotoPistol) camera).Shoot();//Gun.Shoot()
- pp.Shoot(target); //Gun.Shoot(target)
- camera.Shoot(target); //Camera.Shoot(target)
- public class PhotoPistol : Gun /* ,Camera */
- { PhotoPistolCamera camera;
- public PhotoPistol() {camera = new PhotoPistolCamera();}
- public void Focus() { camera.Focus(); }
- class PhotoPistolCamera : Camera { public override Focus() { }}
- public static Camera implicit operator(PhotoPistol p)
- { return p.camera;
- }
- }
- /// This trait declares default methods of IList<T>
- public trait DefaultListMethods<T> : IList<T>
- {
- // Methods without bodies must be implemented by another
- // trait or by the class
- public void Insert(int index, T item);
- public void RemoveAt(int index);
- public T this[int index] { get; set; }
- public int Count { get; }
- public int IndexOf(T item)
- {
- EqualityComparer<T> comparer = EqualityComparer<T>.Default;
- for (int i = 0; i < Count; i++)
- if (comparer.Equals(this[i], item))
- return i;
- return -1;
- }
- public void Add(T item)
- {
- Insert(Count, item);
- }
- public void Clear()
- { // Note: the class would be allowed to override the trait
- // with a better implementation, or select an
- // implementation from a different trait.
- for (int i = Count - 1; i >= 0; i--)
- RemoveAt(i);
- }
- public bool Contains(T item)
- {
- return IndexOf(item) != -1;
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- foreach (T item in this)
- array[arrayIndex++] = item;
- }
- public bool IsReadOnly
- {
- get { return false; }
- }
- public bool Remove(T item)
- {
- int i = IndexOf(item);
- if (i == -1)
- return false;
- RemoveAt(i);
- return true;
- }
- System.Collections.IEnumerator
- System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- IEnumerator<T> GetEnumerator()
- {
- for (int i = 0; i < Count; i++)
- yield return this[i];
- }
- }
- class MyList<T> : MyBaseClass, DefaultListMethods<T>
- {
- public void Insert(int index, T item) { ... }
- public void RemoveAt(int index) { ... }
- public T this[int index] {
- get { ... }
- set { ... }
- }
- public int Count {
- get { ... }
- }
- }
- interface I
- {
- void F();
- void G();
- }
- class DefaultI : I
- {
- void F() { ... }
- void G() { ... }
- }
- class C : I = DefaultI
- {
- public void F() { ... } // implements I.F
- }
Add Comment
Please, Sign In to add comment