这是什么意思@Override? [英] What does @Override mean?
问题描述
public class NaiveAlien extends Alien
{
@Override
public void harvest(){}
}
我想了解我的朋友的code和我不明白的语法,@覆盖在code。这是什么做的,我们为什么需要编码?
谢谢你。
I was trying to understand my friend's code, and I do not get the syntax, @Override in the code. What does that do and why do we need in coding? Thanks.
推荐答案
这是编译器,让它知道你重写父类的(在Java 6或接口)的方法。一个提示
It's a hint for the compiler to let it know that you're overriding the method of a parent class (or interface in Java 6).
如果编译器检测到没有的功能覆盖,它会提醒你(或错误)。
If the compiler detects that there IS no function to override, it will warn you (or error).
这是非常有用的快速识别错别字或API的变化。假设你想重写父类的方法收获()
,但拼 harvset()
,你的程序会悄悄地调用基类,没有 @覆盖
,你就不会对此有任何警告。
This is extremely useful to quickly identify typos or API changes. Say you're trying to override your parent class' method harvest()
but spell it harvset()
, your program will silently call the base class, and without @Override
, you wouldn't have any warning about that.
同样的,如果你使用一个库,并在库的版本2,收获()
已被修改为采取一个整数参数,您将不再覆盖它。同样, @覆盖
会很快告诉你。
Likewise, if you're using a library, and in version 2 of the library, harvest()
has been modified to take an integer parameter, you would no longer override it. Again, @Override
would quickly tell you.
这篇关于这是什么意思@Override?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!