1:java中多态的三要素继承,重写,父类引用指向子类对象
看下面最简单的多态例子:
public class Man { public static void main(String[] args) { Animal c = new Cat("Tom", "黑色"); c.enjoy(); // Dog d = new Dog("大黄", "黑色"); // Man m = new Man("苍井空",c); // m.MyPetEnjoy(); } } class Animal { String name; Animal(String name) { this.name = name; } public void enjoy() { System.out.println("动物叫了"); } } class Cat extends Animal { String Color; public Cat(String n, String c) { super(n); Color = c; } public void enjoy() { System.out.println("我是" + name + " 喵喵喵。。。。。。"); } } class Dog extends Animal { String Color; public Dog(String name, String color) { super(name); Color = color; } public void enjoy() { System.out.println("我是" + name + " 汪汪汪。。。。。。"); } }