简体   繁体   English

关于@Override注解的问题

[英]Question about @Override annotation

Im new to android development. 我是android开发的新手。 I would like to know why do we have to precede every overriden method with @Override annotation in android ?. 我想知道为什么我们必须在android中的每个重写方法前加上@Override注释? In regular java this is not a requirement. 在常规Java中,这不是必需的。

Please Help Thanks 请帮忙谢谢

The @Override notation is used so the compiler can warn you if the method-signature isn't the same as the super class method-signature. 使用@Override表示法,以便如果方法签名与超类方法签名不同,则编译器可以警告您。 It can warn you about tedious bugs, and it is not required in Android, but it is good practice both in "normal" Java programming and Android Programming. 它可以警告您乏味的错误,并且在Android中不是必需的,但是在“常规” Java编程和Android编程中都是好的做法。

If you in "normal" Java had misspelled the toString-method eg public String toString(int n) {...} and you had the @Override the compiler will warn you because you are not overriding a method in the superclass. 如果您在“普通” Java中拼写了toString方法,例如public String toString(int n) {...}并且您使用了@Override ,编译器会警告您,因为您没有覆盖超类中的方法。

It's a best practice and it's safe. 这是最佳做法,而且很安全。 Assume that you have a class: 假设您有一堂课:

public class MyClass {
   ...
   ...
   public void doSomething() {
      ...
      ...
   }
}

Now assume you extend it: 现在假设您扩展它:

public class MyExtendedClass extends MyClass {
   ...
   ...
   public void doSomthing() {
     ...
   }
}

This code will compile, but you'll have problems because you actually haven't overridden the method! 这段代码可以编译,但是会遇到问题,因为您实际上没有覆盖该方法! Note the misspelling. 注意拼写错误。 Now if you annotate it with @Override : 现在,如果您使用@Override对其进行注释:

public class MyExtendedClass extends MyClass {
   ...
   ...
   @Override
   public void doSomthing() {
     ...
   }
}

The java compiler will complain because you are trying to override a method that does not exist in the base class. Java编译器会抱怨,因为您试图覆盖基类中不存在的方法。 Essentially using @Override lets you catch problems like these at compile-time. 本质上,使用@Override可使您在编译时捕获此类问题。

This is especially useful when you refactor. 重构时,此功能特别有用。 If you change a method signature or a name, but don't have the @Override annotation, some methods may slip-by leading to hard-to-find bugs (of course, with modern IDE's a lot of this pain is mitigated, but still). 如果您更改方法签名或名称,但没有@Override批注,则某些方法可能会@Override ,从而导致难以发现的错误(当然,对于现代IDE来说,可以减轻很多这种麻烦,但是,仍然)。 If you judiciously use the @Override annotation, you will now get compiler errors and so you will be able to fix the method signatures in your derived classes. 如果您明智地使用@Override批注,则现在会遇到编译器错误,因此可以在派生类中修复方法签名。

Because it is a good programming practice. 因为这是一种好的编程习惯。 Overriding a method without using @Override makes for a potentially difficult-to-find bug in the future if the base method's signature changes (I've wasted hours on these sorts of bugs before). 如果基本方法的签名发生更改,则在不使用@Override情况下覆盖方法会在将来导致潜在的难以发现的错误(我之前在此类错误上已经浪费了数小时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM