首页 > 代码库 > 多态的运行

多态的运行

 1 public class Duotai { 2     void eat(){ 3         System.out.println("动物在进食"); 4     } 5     void roam(){ 6         System.out.println("动物在活动"); 7     } 8     public static void main(String[] args){ 9         Duotai[] animals = new Duotai[3];10         animals[0] = new Lions();11         animals[1] = new Hippo();12         animals[2] = new Wolf();13         for(int i = 0;i<animals.length;++i){14             animals[i].eat();15             animals[i].roam();16         }17     }18 }19 class Hippo extends Duotai{20 }21 class Wolf extends Duotai{22 }23 class Lions extends Duotai{24 }

技术分享

多态的运行