本文介绍了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.

推荐答案

@Override是。它告诉编译器以下方法其超类。例如,假设你实现了一个Person类。

@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) {
      ...
   }
}

person类有一个equals()方法。 equals方法已在Person的超类等于( )。但是如果你试图使用@Override注释覆盖equals:

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) {
   ...
}

现在编译器知道你有错误。你想要覆盖,但你没有。因此,使用@Override注释的原因是显式声明方法覆盖。

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.

这篇关于Android Studio中@override的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:19