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...

posted @ 2013-02-10 22:36  wouldguan  阅读(355)  评论(0编辑  收藏  举报