Android中的@Override注解
我是 Java 和 Android 编程新手。我遇到的问题是,在浏览了几本书、论坛和网站后,我对 @override 注释的作用并没有清楚的了解。我理解它意味着一个方法被覆盖。但为什么在android中需要它。我很少在java的源代码中看到它,但在android中总是看到它。
I am new to Java and Android programming. The issue that I am having is that after surfing several books, forum and websites, I do not have clear understanding of what the @override annotation does. I understand it signifies when a method is being over ridden. but why is it needed in android. I see it rarely in source code for java, but all the time in android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可能很少在旧的 Java 源代码中看到它,因为它是一项相当新的创新 - 而 Android 代码从定义上来说几乎是更新的。
实际上,它是一个安全网 - 它告诉编译器您正在尝试覆盖某些内容 - 因此,如果方法没有覆盖任何内容,例如由于名字中有拼写错误。这就像
override
是一个关键字,是 C# 中方法声明的一部分。它可以帮助您明确自己正在做的事情,这有助于防止错误,并使您的代码对未来的读者更清晰。You may see it rarely in old Java source code, because it's a fairly recent innovation - whereas Android code is more recent pretty much by definition.
It's a safety net, really - it tells the compiler that you're trying to override something - so please fail if the method doesn't override anything, e.g. due to a typo in the name. It's just like
override
being a keyword which is part of the method declaration in C#. It helps you to be explicit about what you're doing, which helps to prevent mistakes and also makes your code clearer to future readers.好吧,如果您了解 @Override 注释的基本知识,那么它只是满足两个目的的元数据信息:
描述它正在被重写的方法,因为它是在接口或扩展类中定义的
帮助您正确识别本地声明的方法以及重写的方法,因为在 android 中您可能会从 Fragment、Activity、Services、BroadcastReceiver 等扩展,这是一个很好的实践那将会让你的代码更干净。
至于你在 Java 风格中看不到它的问题,这不仅是因为它是一个相当新的功能(自 2005 年以来就不是那么新了),而且因为你在 java 中没有重写太多方法,至少在 Swing 中没有。或 SWT,最常见的是您会在 TableModel 中找到它们,或者当它是扩展 HttpServlet 类中的 Servlet 时
Well if you understand the basic of @Override annotation it's simply metadata information that fill two purposes:
Describe that the method it's being override because it was defined in a interface or a extended class
Helps you identify correctly which method are locally declared and which are overriding since in android you will probably extends from Fragment, Activity, Services, BroadcastReceiver, etc it is a good practice that will make your code cleaner.
As for the issue that you don't see it in Java style, it is not only because it's a fairly new Feature (not that new since 2005), but because you don't override method that much in java at least not in Swing or SWT, most commonly you will find them in TableModel or when it's Servlet in the extended HttpServlet classes
@Override
表示您想在子类中重新定义超类的函数。阅读有关抽象方法、类、多态性的更多信息。@Override
means that you want to re-define function from superclass in child class. Read more about abstract methods, classes, polymorphism.因为您重写了很多方法,例如;当您创建
Activity
(扩展Activity
)时,您需要重写方法onCreate(Bundle savingInstanceState) 来初始化您的 Activity,依此类推,每次重写母类的方法时,eclipse 都会添加注释
@override
because you override a lot of methods , example ;when you create your
Activity
( which extendsActivity
) you need to override the methodonCreate( Bundle savedInstanceState)
to initialize yourActivity
, and so on , every time you override a method from a mother Class , eclipse add the annotation@override
我对 Android 编程几乎一无所知,但 @Override 注释用于表明该方法重写超类方法(或实现 Java 1.6 中的接口方法)。除了注释的纯粹文档含义之外,如果该方法实际上没有覆盖任何超类方法(当您认为它会覆盖任何超类方法时),至少 Eclipse 还会向您发出警告(或将其标记为错误,具体取决于您的设置)(从而防止出现一些令人沮丧的错误) -打猎)。也许 Android 编译器默认要求所有重写方法都用 @Override 进行注释。
I know next to nothing about Android-programming, but the @Override-annotation is used to show that the method overrides a supeclass method (or implements an interface method in Java 1.6). Besides the purely documentational meaning of the annotation, at least Eclipse also warns you (or marks it an error, depending on your settings) if the method doesn't actually override any superclass method, when you think it does (thus preventing some frustrating bug-hunting). Maybe the Android-compiler just by default requires all overriding methods to be annotated with @Override.
java 和 android 都不需要它。
你经常在android程序中看到它,因为当你创建一个新的活动、方法等时,eclipse会自动添加它。
如果删除 @Override 注释,则不会发生任何情况。
It is not needed for both java and android.
You see it often in android programs because eclipse adds it automatically when you create a new activity, method, etc.
If you delete the @Override annotation nothing will appen.
@overide类似于c++中的虚拟方法,android中的Oncreate方法更像是wxWidget中的virtual bool Oninit(),这个方法被android(Oncreate)用来构建你的GUI,所以因为GUI与编程者不同Oncreate 中的代码也发生了变化,这就是我们必须@overide 它的原因。
我们@overide 库使用的并且我们想要自定义的任何方法。 Oncreat() 和 Oninit() 一样,也是我们想要自定义的方法。
@overide is something like a virtual method in c++, Oncreate method in android is more like the virtual bool Oninit() in wxWidget, this method is used by android (Oncreate) to build your GUI so because GUI is different from who is programming the code in Oncreate change too this is why we have to @overide it.
We @overide any method that is used by the library and that we want to customize. Oncreat() like Oninit() are those method we want to customize too.
@override
表示您正在从可用类中借用方法,并且这些方法不是由程序员定义的。@override
means you are borrowing methods from available class and are not defined by the programmer.