Android Studio中@override的含义
我对 Android Studio 完全陌生,我想知道 Android Studio 中 @Override
语句的用途。
I am completely new to Android Studio and I want to know the purpose of the @Override
statement in Android Studio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
@Override 是一个 Java 注释。它告诉编译器以下方法覆盖其超类。例如,假设您实现一个 Person 类。
person 类有一个 equals() 方法。 equals 方法已在 Person 的超类 Object< 中定义/a>.因此,上面的 equals() 实现是对 Persons 的 equals() 的重新定义。也就是说,Person 重写了 equals()。
在没有显式注释的情况下重写方法是合法的。那么@Override注解有什么用呢?如果您不小心尝试以这种方式覆盖 equals() 该怎么办:
上面的情况有一个错误。您本想覆盖 equals() 但您没有。为什么?因为真正的 equals() 获取一个 Object 作为参数,而你的 equals() 获取一个 Person 作为参数。编译器不会告诉您有关该错误的信息,因为编译器不知道您想要覆盖。据编译器所知,您实际上是要 重载 equals( )。但是,如果您尝试使用 @Override 注释覆盖 equals:
现在编译器知道您有错误。你想覆盖但你没有。所以使用@Override注解的原因是显式声明方法重写。
@Override is a Java annotation. It tells the compiler that the following method overrides a method of its superclass. For instance, say you implement a Person class.
The person class has an equals() method. The equals method is already defined in Person's superclass Object. Therefore the above implementation of equals() is a redefinition of equals() for Persons. That is to say, Person overrides equals().
It is legal to override methods without explicitly annotating it. So what is the @Override annotation good for? What if you accidentally tried to override equals() that way:
The above case has a bug. You meant to override equals() but you didn't. Why? because the real equals() gets an Object as a parameter and your equals() gets a Person as a parameter. The compiler is not going to tell you about the bug because the compiler doesn't know you wanted to override. As far as the compiler can tell, you actually meant to overload equals(). But if you tried to override equals using the @Override annotation:
Now the compiler knows that you have an error. You wanted to override but you didn't. So the reason to use the @Override annotation is to explicitly declare method overriding.
它是一个 Java 注释(不是 Android 特定的)。你用它来表示这个方法重写一个方法。
使用它的原因是当你创建一个方法时你打算重写一个方法,但由于某些错误,它并没有捕获错误,例如方法名称中的拼写错误,方法签名中的错误等等。例如,有时开发者会这样:
作者本打算重写超类的
equals
方法,但事实并非如此(参数类型应为Object
) 。该程序可以正常编译,但不会按照作者的意图使用 Foo.equals 。编译现在会给出错误,因为该方法不会覆盖另一个方法。这可以及早指出问题,并有望节省一些跟踪问题的调试时间。
Its a Java annotation (not Android-specific). You use it to mean for this method to override a method.
The reason to use it is to catch errors when you create a method you intend to override a method, but through some error, it does not, e.g. typo in the method name, an error in the method signature, etc.. For example, sometimes developers to this:
The author intended this to override the superclass'
equals
method, but it does not (The parameter type should beObject
). The program will compile fine, but will not useFoo.equals
as the author intended.The compile will now give an error because the method does not override another method. This points out the problem early and hopefully saves some debugging time tracking down the problem.
它是一个 Java 注释,告诉编译器该方法旨在重写超类中的方法。这不是绝对必要的,但确实有助于在编译时捕获错误。
It's a Java annotation that tells the compiler that the method is intended to override a method from the superclass. It's not strictly necessary, but does help to catch errors at compile time.
@Override
< /a> 是一个 java注释
。另请参阅
示例:
@Override
is an javaannotation
.Also See
Example:
它是 Java 中的注释。它标记该方法旨在重写超类中的方法。
It's an annotation in Java. It marks that the method is meant to override a method in a superclass.
这是一个特殊的、编译器保留的 Java 注释,表示您正在重写超类方法。
That's a special, compiler reserved, Java annotation that denotes that you are overriding a superclass method.