java @override 超类的方法是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7654975/
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
What does it mean to @override the method of a super class?
提问by user979431
Let's say I have a method called mymethod()
假设我有一个方法叫做 mymethod()
and this method overrides the method of the super class method.
并且这个方法覆盖了超类方法的方法。
What does it mean to override a method?
重写一个方法是什么意思?
Does that mean mymethod()
ignores everything that is in the method of the superclass, or does that means mymethod()
also includes everything in the superclass method?
这是否意味着mymethod()
忽略mymethod()
超类方法中的所有内容,或者这是否意味着还包括超类方法中的所有内容?
When overriding a method, can I only override the methods of the same name, or I can override methods of any name?
覆盖方法时,是只能覆盖同名的方法,还是可以覆盖任何名称的方法?
thanks.
谢谢。
回答by wkl
An example:
一个例子:
public class Base {
public void saySomething() {
System.out.println("Hi, I'm a base class");
}
}
public class Child extends Base {
@Override
public void saySomething() {
System.out.println("Hi, I'm a child class");
}
}
Now assume we have a main
function somewhere...
现在假设我们在main
某处有一个函数......
public static void main(String [] args) {
Base obj = new Child();
obj.saySomething();
}
When this runs, it will call Child
's version of saySomething
, because you overrode the parent's version by giving a new version of the function in Child
.
当它运行时,它将调用Child
的版本saySomething
,因为您通过在 中提供函数的新版本来覆盖父版本Child
。
The @Override
annotation allows other developers (and you, when you forget) to know that this method overrides something in a base class/interface, and it also allows the compiler to yell at you if you're not actually overriding anything in a base class. For example, if you got the number of arguments wrong for a function, the compiler will give you an error saying your @Override
is incorrect.
该@Override
注释允许其他开发人员(和你,当你忘记了)知道该方法覆盖基类/接口东西,这也让编译器在你叫喊,如果你没有真正覆盖在基类东西。例如,如果函数的参数数量有误,编译器会提示您@Override
错误。
For example:
例如:
public class Child extends Base {
@Override
public void saySomething(int x) {
System.out.println("I'm a child and x is: " + x);
}
}
The compiler will yell at you because this version of saySomething
takes an argument, but the parent's version doesn't have an argument, so you're @Override
-ing something that's not in the parent.
编译器会对你大喊大叫,因为这个版本的saySomething
有一个参数,但父版本没有参数,所以你正在@Override
-ing 一些不在父版本中的东西。
On super
在超级
The Child
version of saySomething
will not invoke the Base
version, you have to do it yourself with super.method()
.
的Child
版本saySomething
不会调用Base
版本,你必须自己用super.method()
.
For example:
例如:
public class Child extends Base {
@Override
public void saySomething() {
super.saySomething();
System.out.println("I'm also a child");
}
}
If you ran the main
and used this Child
class, it would print out I'm a base
and I'm also a child
.
如果你运行main
并使用这个Child
类,它会打印出I'm a base
和I'm also a child
。
回答by Vivin Paliath
Overriding means that when you call a method on your object, your object's method is called instead of the super class. The @Override
annotation is something you use to make sure that you are overriding the correct method of the superclass. If you annotate a method that does not exist in the superclass, the Java compiler will give you an error. This way you can be sure that you are overriding the correct methods. This is especially useful in cases like this:
覆盖意味着当您在对象上调用方法时,将调用对象的方法而不是超类。该@Override
注释是使用,以确保你重写父类的方法正确的东西。如果你注解一个在超类中不存在的方法,Java 编译器会给你一个错误。这样您就可以确保您覆盖了正确的方法。这在这样的情况下特别有用:
public class MyClass {
...
public boolean equals(MyClass myClass) {
...
}
}
There is a logic-bug in the code above. You haven't actually overridden the Object
class's equals
method. If you add the @Override
annotation:
上面的代码中有一个逻辑错误。您实际上还没有覆盖Object
类的equals
方法。如果添加@Override
注释:
public class MyClass {
...
@Override
public boolean equals(MyClass myClass) {
...
}
}
The Java compiler will now complain because there is no corresponding method in the parent class. You'll then know that the correct solution is:
Java 编译器现在会抱怨,因为父类中没有相应的方法。然后您就会知道正确的解决方案是:
public class MyClass {
...
@Override
public boolean equals(Object o) {
...
}
}
To call the parent class's method, you can call super.overriddenMethod()
where overriddenMethod
is the name of the method you have overridden. So if you want to do something in addition to what the parent class already does, you can do something like this:
要调用父类的方法,您可以调用super.overriddenMethod()
whereoverriddenMethod
是您已覆盖的方法的名称。所以如果你想在父类已经做的事情之外做一些事情,你可以做这样的事情:
public class MyClass {
...
@Override
public void overriddenMethod() {
super.overriddenMethod();
/* whatever additional stuff you want to do */
}
}
回答by Joshua Smith
If an inheriting class has on override method of the same name as the parent class it will be called instead of the one in the parent class. This only works if the names are the same, and of course if the signature of the method matches your call to the method.
如果继承类具有与父类同名的重写方法,它将被调用而不是父类中的方法。这仅在名称相同时才有效,当然,如果方法的签名与您对该方法的调用相匹配。
回答by Gyan aka Gary Buyn
What does it mean to override a method?
重写一个方法是什么意思?
It means you replace the super class definition of the method with your own definition.
这意味着您用自己的定义替换方法的超类定义。
does that mean mymethod() ignores everything that is in the method of the super class? or does that means mymethod() also includes everything in the superclass method?
这是否意味着 mymethod() 忽略超类方法中的所有内容?或者这是否意味着 mymethod() 还包括超类方法中的所有内容?
You can choose whether to include the super class definition within your definition. To include it, you need to call super.mymethod()
within mymethod()
.
您可以选择是否在定义中包含超类定义。要包含它,您需要super.mymethod()
在mymethod()
.
and when overriding a method, can I only override the methods of the same name, or I can override methods of any name?
并且在覆盖方法时,我可以只覆盖同名的方法,还是可以覆盖任何名称的方法?
To override a method, you must supply a method in the sub class with the same signature (which means the same name, parameters and return type).
要覆盖一个方法,您必须在子类中提供一个具有相同签名的方法(这意味着相同的名称、参数和返回类型)。
As a side note, the @Override
annotation in your question does not actually cause your method to override another method. It causes a compile-time error if a method annotated with it does not have a signature matching a public or protected method of a super class (or interface as of 1.6).
作为旁注,@Override
您问题中的注释实际上并没有导致您的方法覆盖另一个方法。如果用它注释的方法没有与超类(或 1.6 版本的接口)的公共或受保护方法匹配的签名,则会导致编译时错误。
回答by TofuBeer
I once had a student come to ask me why his code wasn't working. He had spent several days wondering why he could put something into a collection but was not able to find it. His code was something like:
我曾经有一个学生来问我为什么他的代码不起作用。他花了好几天想知道为什么他可以将某些东西放入收藏中却找不到它。他的代码是这样的:
public int hashcode()
instead of:
代替:
public int hashCode()
So the hashCode method never got called.
所以 hashCode 方法从未被调用。
Adding @Overrides to a method makes it clear that you are overriding the method AND make sure that you really are overriding a method.
将@Overrides 添加到方法可以清楚地表明您正在覆盖该方法并确保您确实正在覆盖一个方法。
回答by Matt
When you override a method of the super class, calling that method on your object calls its method instead of that of the super class.
当您覆盖超类的方法时,在您的对象上调用该方法将调用其方法而不是超类的方法。
You can call the super class's method (despite having overridden it) using super.methodName()
. A common reason for this is when the overridden method would otherwise reimplement the super class method and add additional code specific to the extending class (public void methodName() { super.methodName(); /* more code */ }
).
您可以使用super.methodName()
. 造成这种情况的一个常见原因是,当重写的方法以其他方式重新实现超类方法并添加特定于扩展类 ( public void methodName() { super.methodName(); /* more code */ }
) 的附加代码时。
@Override
annotation allows you to cause warning at compile time if the method isn't actually overriding anything. It isn't necessary, but these warning are a hit to you that you might have got the signature wrong in the extending class, forgot to implement the method at all in the super class, or some other silly mistake.
@Override
如果该方法实际上没有覆盖任何内容,则注释允许您在编译时发出警告。这不是必需的,但是这些警告对您来说是一个打击,您可能在扩展类中得到了错误的签名,根本忘记了在超类中实现该方法,或者其他一些愚蠢的错误。