Http://www.cnblogs.com/wqq0402/p/6180685.html
Package test05;
public class Duotai_test02 {
/** multiple objects, one pattern
* Tiger, Lion, Snake→animal
*, multiple objects, a form (type).
* Different objects, showing the same pattern after.
* The same functionality can be achieved.
*
* In this way, multiple sub-classes are required to correspond to a parent class.
* It is often implemented by inheriting the parent class with (multiple) subclasses.
*
* @param args
*/
public static void Main (string[] args) {
Animal Animal = new Cat ();//abstract reference to child class object
Animal.eat ();
Animal.sheep ();
Animal animal2 = new Dog ();
Animal2.eat ();
Animal2.sheep ();
Animal animal3 = new Snack ();
Animal3.eat ();
Animal3.sheep ();
duotai_test02 dt = new duotai_test02 ();
Dt.test (New Cat ());
Dt.test (New Dog ());
Dt.test (New Snack ());
}
public void Test (Animal animals) {
Animals.eat ();
Animals.sheep ();
}
}
Abstract class Animal {//Abstraction classes
String name;
int age;
public abstract void Eat ();//abstract method
public abstract void sheep ();//abstract method
}
Class Cat extends Animal {
public void Eat () {//method override
System.out.println ("Cat Eats");
}
public void sheep () {
System.out.println ("Cat Sleeps");
}
}
Class Dog extends Animal {
public void Eat () {//method override
System.out.println ("Dog Eats");
}
public void sheep () {
System.out.println ("The Dog Sleeps");
}
}
Class Snack extends Animal {
public void Eat () {//method override
System.out.println ("Snake Eats");
}
public void sheep () {//method override
System.out.println ("Sleeping Snakes");
}
}
Java polymorphism 2