public class Pet{void Bathe();void Train(Trick t);}
public class Dog{private Pet pet;
public void Bathe() { pet.Bathe(); }public void Train(Trick t) { pet.Train(t); }}
public class Cat{private Pet pet;
public void Bathe() { pet.Bathe(); }public void Train(Trick t) { pet.Train(t); }}
public abstract class CloneableType{// Only derived types can support this// "polymorphic interface." Classes in other// hierarchies have no access to this abstract// member.public abstract object Clone();}
public abstract class Dog{public virtual void Bark(){Console.WriteLine("Base Class implementation of Bark");}}
public class GoldenRetriever : Dog{// the Bark method is inherited from the Dog class}
public class Poodle : Dog{// here we are overriding the base functionality of Bark with our new implementation// specific to the Poodle classpublic override void Bark(){Console.WriteLine("Poodle's implementation of Bark");}}
// Add a list of dogs to a collection and call the bark method.
void Main(){var poodle = new Poodle();var goldenRetriever = new GoldenRetriever();
var dogs = new List<Dog>();dogs.Add(poodle);dogs.Add(goldenRetriever);
foreach (var dog in dogs){dog.Bark();}}
// Output will be:// Poodle's implementation of Bark// Base Class implementation of Bark
//
// Create ISwimable interfacepublic interface ISwimable{public void Swim();}
// Have Human implement ISwimable Interfacepublic class Human : ISwimable
public void Swim(){//Human's implementation of SwimConsole.WriteLine("I'm a human swimming!");}
// Have Duck implement ISwimable interfacepublic class Duck: ISwimable{public void Swim(){// Duck's implementation of SwimConsole.WriteLine("Quack! Quack! I'm a Duck swimming!")}}
//Now they can both be used in places where you just need an object that has the ability "to swim"
public void ShowHowYouSwim(ISwimable somethingThatCanSwim){somethingThatCanSwim.Swim();}
public void Main(){var human = new Human();var duck = new Duck();
var listOfThingsThatCanSwim = new List<ISwimable>();
listOfThingsThatCanSwim.Add(duck);listOfThingsThatCanSwim.Add(human);
foreach (var something in listOfThingsThatCanSwim){ShowHowYouSwim(something);}}
// So at runtime the correct implementation of something.Swim() will be called// Output:// Quack! Quack! I'm a Duck swimming!// I'm a human swimming!
abstract class PetBase implements IPet {// Add all abstract methods in IPet interface and keep base class clean.Base class will contain only non abstract methods and static methods.}
abstract class PetBase implements IPet {// Add all abstract methods in IPet}
/*If ISheds,IBarks is common for Pets, your PetBase can implement ISheds,IBarks.Respective implementations of PetBase can change the behaviour in their concrete classes*/
abstract class PetBase implements IPet,ISheds,IBarks {// Add all abstract methods in respective interfaces}
1. You have a general interface (eg IPet)2. You have a implementation that is less general (eg Mammal)3. You have many concrete members (eg Cat, Dog, Ape)
抽象类定义了具体类的默认共享属性,但强制执行接口。例如:
public interface IPet{
public boolean hasHair();
public boolean walksUprights();
public boolean hasNipples();}
现在,因为所有的哺乳动物都有毛发和乳头(AFAIK,我不是动物学家),我们可以把它卷成抽象的基类
public abstract class Mammal() implements IPet{
@overridepublic walksUpright(){throw new NotSupportedException("Walks Upright not implemented");}
@overridepublic hasNipples(){return true}
@overridepublic hasHair(){return true}
然后具体的类仅仅定义它们直立行走。
public class Ape extends Mammal(){
@overridepublic walksUpright(return true)}
public class Catextends Mammal(){
@overridepublic walksUpright(return false)}