zoukankan      html  css  js  c++  java
  • Java @Override 注解

    @Override注解,不是关键字,但可以当关键字使用,可以选择添加这个注解,在你不留心重载而并非复写了该方法时,编译器就会产生一条错误:The method doh(Milhouse) of type Bart must override or implement a supertype method

    // reusing/hide.java
    //owrloading a base method name in a derived
    package object;
    
    import static net.mindview.util.Print.*;
    
    class Homer{
        char doh(char c)
        {
            print("doh(char)");
            
            return 'd';
        }
        float doh(float f)
        {
            print("doh(float");
            return 1.f;
        }
    }
    class Milhouse{}
    class Bart extends Homer{
        void doh(Milhouse m)
        {
            print("doh(Milhouse");
        }
        @Override char doh(char c) //基类必须也有char doh(char c)方法
        {
            print("I have not heavy load");
            return 'a';
        }
    }
    
    public class Hide {
        public static void main(String[] args)
        {
            Bart b = new Bart();
            b.doh(1);
            b.doh('x');
            b.doh(1.0f);
            b.doh(new Milhouse());
        }
    }/* output:
    doh(float
    I have not heavy load
    doh(float
    doh(Milhouse
    *///~
  • 相关阅读:
    BD String
    1114
    1083
    1084
    1108
    1087
    1145
    1217
    1164
    反射
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10189882.html
Copyright © 2011-2022 走看看