Android Studio中@override的含义

发布于 2024-10-12 15:35:05 字数 81 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

人间☆小暴躁 2024-10-19 15:35:05

@Override 是一个 Java 注释。它告诉编译器以下方法覆盖超类。例如,假设您实现一个 Person 类。

public class Person {
   public final String firstName;
   public final String lastName;

   //some methods

   @Override public boolean equals(Object other) {
      ...
   }
}

person 类有一个 equals() 方法。 equals 方法已在 Person 的超类 Object< 中定义/a>.因此,上面的 equals() 实现是对 Persons 的 equals() 的重新定义。也就是说,Person 重写了 equals()。

在没有显式注释的情况下重写方法是合法的。那么@Override注解有什么用呢?如果您不小心尝试以这种方式覆盖 equals() 该怎么办:

public boolean equals(Person other) {
   ...
}

上面的情况有一个错误。您本想覆盖 equals() 但您没有。为什么?因为真正的 equals() 获取一个 Object 作为参数,而你的 equals() 获取一个 Person 作为参数。编译器不会告诉您有关该错误的信息,因为编译器不知道您想要覆盖。据编译器所知,您实际上是要 重载 equals( )。但是,如果您尝试使用 @Override 注释覆盖 equals:

@Override public boolean equals(Person other) {
   ...
}

现在编译器知道您有错误。你想覆盖但你没有。所以使用@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.

public class Person {
   public final String firstName;
   public final String lastName;

   //some methods

   @Override public boolean equals(Object other) {
      ...
   }
}

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:

public boolean equals(Person other) {
   ...
}

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:

@Override public boolean equals(Person other) {
   ...
}

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.

旧话新听 2024-10-19 15:35:05

它是一个 Java 注释(不是 Android 特定的)。你用它来表示这个方法重写一个方法。

使用它的原因是当你创建一个方法时你打算重写一个方法,但由于某些错误,它并没有捕获错误,例如方法名称中的拼写错误,方法签名中的错误等等。例如,有时开发者会这样:

class Foo { 
    public boolean equals(Foo other) {
       ...

作者本打算重写超类的 equals 方法,但事实并非如此(参数类型应为 Object) 。该程序可以正常编译,但不会按照作者的意图使用 Foo.equals 。

class Foo {
    @Override
    public boolean equals(Foo other) {
       ...

编译现在会给出错误,因为该方法不会覆盖另一个方法。这可以及早指出问题,并有望节省一些跟踪问题的调试时间。

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:

class Foo { 
    public boolean equals(Foo other) {
       ...

The author intended this to override the superclass' equals method, but it does not (The parameter type should be Object). The program will compile fine, but will not use Foo.equals as the author intended.

class Foo {
    @Override
    public boolean equals(Foo other) {
       ...

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.

憧憬巴黎街头的黎明 2024-10-19 15:35:05

它是一个 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.

迟到的我 2024-10-19 15:35:05

@Override< /a> 是一个 java 注释

指示方法声明旨在覆盖超类中的方法声明。如果一个方法使用此注释类型进行注释,但没有重写超类方法,则编译器需要生成错误消息。

另请参阅

示例:

class Parent{
    public void foo(){

    }
}

class Child extends Parent{
    //compile perfect !
    @Override
    public void foo(){

    }
    //will show compile time error message here as it is not being overridden 
    @Override
    public void foo(int i){

    }

}

@Override is an java annotation.

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

Also See

Example:

class Parent{
    public void foo(){

    }
}

class Child extends Parent{
    //compile perfect !
    @Override
    public void foo(){

    }
    //will show compile time error message here as it is not being overridden 
    @Override
    public void foo(int i){

    }

}
满身野味 2024-10-19 15:35:05

它是 Java 中的注释。它标记该方法旨在重写超类中的方法。

It's an annotation in Java. It marks that the method is meant to override a method in a superclass.

夜还是长夜 2024-10-19 15:35:05

这是一个特殊的、编译器保留的 Java 注释,表示您正在重写超类方法。

That's a special, compiler reserved, Java annotation that denotes that you are overriding a superclass method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文