@Override 是什么意思? [英] What does @Override mean?

查看:34
本文介绍了@Override 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class NaiveAlien extends Alien
{

    @Override
    public void harvest(){}

}

我试图理解我朋友的代码,但我没有理解代码中的语法 @Override.那有什么作用,为什么我们需要编码?谢谢.

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 更改非常有用.假设您试图覆盖父类的方法 harvest() 但将其拼写为 harvset(),您的程序将无声地调用基类,并且没有 @Override,您不会收到任何警告.

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 中,harvest() 已修改为采用整数参数,您将不再覆盖它.同样,@Override 会很快告诉你.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆