Java Android @Override 用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2950074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android @Override usage
提问by Ian Vink
(Newbie to Java, old time C# guy.)
(Java 新手,C# 老手。)
I have noticed a lot of the use of @Override
in Android example code. I thought that all Java methods were by default "Virtual"?
我注意到很多@Override
在 Android 示例代码中的使用。我认为所有 Java 方法默认都是“虚拟的”?
What then does @Override
do?
那有什么作用@Override
呢?
Example:
例子:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
采纳答案by JRL
It's an annotation that you can use to tell the compiler and your IDE that you intend the method that has that annotation to be an override of a super class method. They have warning/errors in case you make mistakes, for example if you intend to override a method but misspell it, if the annotation is there the IDE or the compiler will tell you that it is notin fact overriding the super class method and thus you can determine why and correct the misspelling.
这是一个注释,您可以使用它来告诉编译器和您的 IDE,您希望具有该注释的方法是超类方法的覆盖。如果您犯了错误,它们会发出警告/错误,例如,如果您打算覆盖一个方法但拼错了它,如果注释在那里,IDE 或编译器会告诉您它实际上并没有覆盖超类方法,因此您可以确定原因并纠正拼写错误。
This is all the more important for Android applications and activities for example, where all of the calls will be based on the activity lifecycle - and if you do not properly override the lifecycle methods they will never get called by the framework. Everything will compile fine, but your app will not work the way you intend it to. If you add the annotation, you'll get an error.
这对于 Android 应用程序和活动尤为重要,例如,所有调用都将基于活动生命周期 - 如果您没有正确覆盖生命周期方法,框架将永远不会调用它们。一切都会很好地编译,但您的应用程序将无法按您预期的方式工作。如果添加注释,则会出现错误。
回答by lostiniceland
The Override-Annotation is just a hint for the compiler that you want to overwrite a certain function. The compiler will then check parent-classes and interfaces if the function exists there. If not, you will get a compile-error.
Override-Annotation 只是提示编译器您要覆盖某个函数。如果函数存在,编译器将检查父类和接口。否则,您将收到编译错误。
Its basically just a safety mechanism.
它基本上只是一个安全机制。
For reference, see this article(override is explained somewhere in the middle)
作为参考,请参阅这篇文章(覆盖在中间的某处进行了解释)
回答by samir vora
This code for the beginner who really want to understand about the @Override
process, this will help you! (Remind inheritance concept of Java.)
此代码适合真正想了解@Override
流程的初学者,这将对您有所帮助!(提醒Java的继承概念。)
For example, the Fish
class might have two subclasses: FreshwaterFish
and SaltwaterFish
.
例如,Fish
该类可能有两个子类:FreshwaterFish
和SaltwaterFish
。
These subclasses would have all the features of the Fish
class, but could further customize the objects through new attributes and behaviors or modified behaviors from the parent class Fish
. For example, the FreshwaterFish
class might include information about the type of freshwater environment lived in (e.g. river, lake, pond, or puddle).
这些子类将具有Fish
该类的所有功能,但可以通过新的属性和行为或来自父类的修改行为进一步定制对象Fish
。例如,FreshwaterFish
该类可能包括有关生活在其中的淡水环境类型(例如河流、湖泊、池塘或水坑)的信息。
Similarly, the SaltwaterFish
class might customize the makeBabyFish()
method such that the fish eats its mate after breeding (as defined in the super class) by using the override mechanism, like this:
类似地,SaltwaterFish
该类可能会makeBabyFish()
使用覆盖机制自定义该方法,以便鱼在繁殖后吃掉它的配偶(如超类中所定义),如下所示:
public class SaltwaterFish extends Fish
{
@Override
public void makeBabyFish(Fish fishSpouse, int numBabies) {
// call parent method
super.makeBabyFish(fishSpouse, numBabies);
// eat mate
eat(fishSpouse);
}
}
回答by vivek
Override is mostly used in case of defining a method.Overriding is similar to way its meaning.
Override 主要用于定义方法的情况。Overriding 类似于 way 的含义。
I will try to explain in very lame way.Suppose if you have Oncreate() Method already defined and have the associated properties with it. and Again when you call Oncreate() method in your code for certain object,the code which you have written now... will override the formally defined property or inherited property of Oncreate() for your application.
我将尝试以非常蹩脚的方式进行解释。假设您已经定义了 Oncreate() 方法并具有与之相关的属性。同样,当您在代码中为某个对象调用 Oncreate() 方法时,您现在编写的代码...将覆盖您的应用程序的正式定义的 Oncreate() 属性或继承属性。