zoukankan      html  css  js  c++  java
  • 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...

  • 相关阅读:
    NACBD
    周总结7
    周总结6
    团队题目——TD课程通
    移动端疫情展示
    周总结5
    每周总结4
    每周总结3
    每周总结2
    求最大子数组
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2909873.html
Copyright © 2011-2022 走看看