简体   繁体   English

java中的@Override注释如何检查方法的拼写错误?

[英]How @Override annotation in java can check misspelling of a method?

I was looking through information on @Override annotation in Stackoverflow . 我正在查看Stackoverflow中有关@Override注释的信息。

I've learned that it overrides a parent method. 我已经知道它会覆盖父方法。 However,I saw some comments saying that @Override can in fact check misspelling, and it is quite useful to put @override annotation before all methods. 但是,我看到一些评论说@Override实际上可以检查拼写错误,并且在所有方法之前放置@override注释是非常有用的。

I want to know how @Override can check misspelling of a method name. 我想知道@Override如何检查方法名称的拼写错误。 (if I understood correctly) (如果我理解正确的话)

Let's I want to override this method: 我想覆盖这个方法:

public class Foo {
    public void foo() { ... }
}

Without @Override , I can still override it: 没有@Override ,我仍然可以覆盖它:

public class Bar extends Foo {
    public void foo() { ... }
}

But if I misspelled it: 但如果我拼错了它:

public class Bar extends Foo {
    public void fooo() { ... }
}

fooo does not override foo . fooo不会覆盖foo If I didn't realise the misspelling, I wouldn't know that foo is actually not overridden. 如果我没有意识到拼写错误,我不会知道foo实际上没有被覆盖。 This can cause bugs. 这可能会导致错误。

If I add @Override to fooo , however: 如果我添加@Overridefooo但是:

public class Bar extends Foo {
    @Override
    public void fooo() { ... }
}

The compiler will tell me that fooo does not override anything. 编译器会告诉我fooo不会覆盖任何东西。 I will see this message and go "But it does override foo ! Why did the compiler say that it doesn't? Oh wait! I misspelled it.". 我会看到这个消息然后去“但它确实覆盖了foo !为什么编译器说它没有?哦等等!我拼错了它。”

Your understanding is wrong. 你的理解是错误的。

First: it's @Override . 第一:它是@Override Java is case sensitive, so yes, the distinction is important. Java区分大小写,所以是的,区别很重要。 Secondly: 其次:

I've learned that it overrides a parent method. 我已经知道它会覆盖父方法。

No. It just marks the fact that the method it is applied to does. 不,它只是标志着它应用的方法的事实。 Just because you slap @Overrides on a method, doesn't magically make it override something. 仅仅因为你在一个方法上@Overrides ,并没有神奇地使它覆盖某些东西。 It'll cause Exceptions if it doesn't. 如果没有,它将导致例外。

As for checking for misspelling: let's say you want to override the toString() method. 至于检查拼写错误:假设您要覆盖toString()方法。

@Override
public String toString() {
  return "myString";
}

will be successful, because toString does exist like that in a parent class (Object). 将成功,因为toString确实存在于父类(Object)中。

However, when you try: 但是,当你尝试:

@Override
public String toSttring() {
  return "one t too many";
}

it will search in parent classes and implemented interfaces. 它将搜索父类和实现的接口。 If it can't find this method signature anywhere, it will throw an Exception. 如果它无法在任何地方找到此方法签名,则会抛出异常。 Seeing as this most likely should have been toString() instead of toSttring() , that's one check. toSttring()这很可能应该是toString()而不是toSttring() ,这是一个检查。

It doesn't, however, just check for spelling. 但是,它不会检查拼写。 It basically checks the method signature (which includes the method name). 它基本上检查方法签名(包括方法名称)。

If you were to try: 如果您尝试:

@Override
public Double toString() {
  return new Double("5");
}

Unless you have added this method yourself in a parentclass of your class, this will fail, since the Objects toString returns a String. 除非您自己在类的父类中添加了此方法,否则这将失败,因为Objects toString返回一个String。

Just like: 就像:

@Override
public String toString(String value) {
  return value;
}

is likely to fail, for the same reasons. 由于同样的原因,很可能会失败。

EDIT: Just for clarification: @Override doesn't do a spellcheck. 编辑:只是为了澄清: @Override没有进行拼写检查。 If you misspelled the method name in the parent class, it won't work if you spell it correctly in the child class and add the Override annotation. 如果您在父类中拼错了方法名称,如果在子类中正确拼写它并添加覆盖注释,它将不起作用。

The answer is simple. 答案很简单。

@Override annotation indicates the compiler that the method below the override annotation is overridden ie, a method with matching signature is expected to be present in one of the parent classes in the hierarchy of the class in which the method is being written at the time of writing the code. @Override注释指示编译器覆盖覆盖注释下面的方法被覆盖,即,具有匹配签名的方法应该出现在类的层次结构中的一个父类中,其中该方法在该类的层次结构中被写入。编写代码。

It checks at compiler time, in the hierarchy (ie, parent class, grand parent class, etc) whether the current method you have written has a method with the same signature. 它会在编译器时间,层次结构(即父类,祖父类等)中检查您编写的当前方法是否具有相同签名的方法。 If not, it raises the error. 如果不是,则会引发错误。 It's not about spell check. 这不是拼写检查。 It's about signature match ie, type and order of arguments along with method name. 它是关于签名匹配,即参数的类型和顺序以及方法名称。

java @Override annotation will make sure any super-class changes in method signature will result in a warning and you will have to do necessary changes to make sure the classes work as expected. java @Override注释将确保方法签名中的任何超类更改都会导致警告,您必须进行必要的更改以确保类按预期工作。

It's better to resolve potential issues at compile time than run-time. 在编译时解决潜在问题比运行时更好。 So always use java @Override annotation whenever you are trying to override a super-class method. 因此,每当您尝试覆盖超类方法时,请始终使用java @Override注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM