java override
Posted rojas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ========================== */
以上是关于java override的主要内容,如果未能解决你的问题,请参考以下文章