java多态例子
package com.temp; import java.util.ArrayList; public class PolymorphismTest { /** * @param args */ public static void main(String[] args) { ArrayList<Human> persons = new ArrayList<Human>(); persons.add(new Male()); persons.add(new Female()); for (Human person : persons) { person.goPee(); } } } abstract class Human { public abstract void goPee(); } class Male extends Human { @Override public void goPee() { System.out.println(this.getClass() + "\tStand up..."); } } class Female extends Human { @Override public void goPee() { System.out.println(this.getClass() + "\tSit down..."); } }
output:
class com.temp.Male Stand up...
class com.temp.Female Sit down...