zoukankan      html  css  js  c++  java
  • Effective Java 36 Consistently use the Override annotation

    Principle

    Use the Override annotation on every method declaration that you believe to override a superclass declaration.

    // Can you spot the bug?

    public class Bigram {

    private final char first;

    private final char second;

    public Bigram(char first, char second) {

    this.first = first;

    this.second = second;

    }

    // This method is not the equals(Object) 's override, it's just a new method

    public boolean equals(Bigram b) {

    return b.first == first && b.second == second;

    }

    public int hashCode() {

    return 31 * first + second;

    }

    public static void main(String[] args) {

    Set<Bigram> s = new HashSet<Bigram>();

    for (int i = 0; i < 10; i++)

    for (char ch = 'a'; ch <= 'z'; ch++)

    s.add(new Bigram(ch, ch));

    System.out.println(s.size()); // it will print 260 not 26.

    }

    }

    The @Override annotation will help you find the issue at the compile time.

    Bigram.java:10: method does not override or implement a method

    from a supertype

    @Override public boolean equals(Bigram b) {

    ^

    // The correct overriding

    @Override public boolean equals(Object o) {

    if (!(o instanceof Bigram))

    return false;

    Bigram b = (Bigram) o;

    return b.first == first && b.second == second;

    }

       

    Note

    It is worth annotating all methods that you believe to override superclass or super interface methods, whether concrete or abstract. For example, the Set interface adds no new methods to the Collection interface, so it should include Override annotations on all of its method declarations, to ensure that it does not accidentally add any new methods to the Collection interface.

       

    Summary

    The compiler can protect you from a great many errors if you use the Override annotation on every method declaration that you believe to override a supertype declaration, with one exception. In concrete classes, you need not annotate methods that you believe to override abstract method declarations(though it is not harmful to do so).

       

  • 相关阅读:
    linux weblogic的sh文件
    linux 安装weblogic(转载)
    linux 安装jdk
    linux 用户和用户组
    测试开发工程师必备软硬能力&高级测试开发工程师需要具备什么能力?
    postman强大的团队协作功能
    requests(一): 发送一个json格式的post请求
    python操作Excel模块openpyxl
    appium环境安装app自动化
    夜神模拟器怎么连接adb
  • 原文地址:https://www.cnblogs.com/haokaibo/p/Consistently-use-the-Override-annotation.html
Copyright © 2011-2022 走看看