码迷,mamicode.com
首页 > 编程语言 > 详细

java override

时间:2016-03-31 14:32:12      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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

 */

 

java override

标签:

原文地址:http://www.cnblogs.com/rojas/p/5340945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!