java override

java override


// 覆盖父类的方法,在子类直接调用子类内部这个方法时 
 public class TestCalc{

  protected static class Wine{
    String name(){return "Wine";}
  }

  protected static class  SparkLingWine extends Wine{
    @Override String name(){return "SparkLingWine";}
  }

  protected static class  Champagne extends SparkLingWine{

    //在方法调用时先会在子类中找覆盖的方法,如果子类中没有则会在父类中去找。有多层继承的话按就近查找
    //@Override String name(){return "Champagne";}  

    @Override String name(){return "Champagne";} 

  }
    
   public static void main(String[] args) 
   {
   
    Wine wines[]={new Wine(),new SparkLingWine(),new Champagne()};
    for (Wine wine:wines) {
      System.out.println(wine.name());
      System.out.println("==========================");
    }

   }

 }   
 /*
Wine
==========================
SparkLingWine
==========================
Champagne
==========================

 */

 

posted on 2016-03-31 13:47  rojas  阅读(234)  评论(0编辑  收藏  举报