java 重新定义方法和覆盖方法有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4831172/
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's the difference between redefining a method and overriding a method?
提问by enon
class DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey battling");
}
}
class FunkyBattler extends DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey battling with bellbottoms");
}
}
doBattle method is supposed to be a redefinition or an override? Oh this is Java by the way.
doBattle 方法应该是重新定义还是重写?哦,顺便说一下,这是Java。
采纳答案by justkt
The term "redefinition" isn't usually used with regards to Java methods and inheritance. There are two terms that are commonly used: "override" as you said, and "overload." To overload in Java is to create two methods in the same class with the same name but different signatures (number and/or types of arguments). For example:
术语“重新定义”通常不用于 Java 方法和继承。有两个常用术语:如您所说的“覆盖”和“过载”。在 Java 中重载是在同一个类中创建两个具有相同名称但不同签名(参数的数量和/或类型)的方法。例如:
public interface MyInterface
{
public int doStuff(int first, int second);
public int doStuff(double only);
}
To override is to do something like what you are doing in your example: create a child class with a method that has the same name andsignature as a method in the parent class that will be used for all instances of the child class but not of the parent class or any other child classes of that parent.
覆盖是做一些类似于你在你的例子中所做的事情:创建一个子类,它的方法与父类中的方法具有相同的名称和签名,该方法将用于子类的所有实例,但不用于子类的所有实例父类或该父类的任何其他子类。
The only issue with your example as it relates to overloading is the use of the keyword static
. Overriding is determined dynamically, but static methods by definition are not.
您的示例与重载有关的唯一问题是关键字的使用static
。覆盖是动态确定的,但静态方法根据定义不是。
回答by Andrzej Doyle
I've never heard of "redefine" as an OO term as applied to Java.
我从未听说过“重新定义”作为适用于 Java 的 OO 术语。
However, the example you give is notoverriding, because static methods are not inherited, but are statically dispatched based on the type of the variable (as opposed to the dynamic dispatch that occurs with member methods).
但是,您给出的示例并未覆盖,因为静态方法不是继承的,而是根据变量的类型静态调度的(与成员方法发生的动态调度相反)。
I wouldn't call it a redefinition, though - you had a method called DonkeyBattler.doBattle
, and you've now defined a compiletely separate method called FunkyBattler.doBattle
.
不过,我不会称其为重新定义——您有一个名为 的方法DonkeyBattler.doBattle
,而您现在定义了一个名为FunkyBattler.doBattle
.
回答by Murthy24
Intention of overriding is actually Redefining the inherited method from the parent class.
覆盖的意图实际上是重新定义从父类继承的方法。
Redefining involves:
重新定义包括:
Replacement
1. **Replacement** is the case in which child class is overriding
The inherited method of parent class with a behavior(functionality) which is completely different from corresponding parent method and a sign for this process is not calling super.method() in the body of child method.
Refinement
2. Refinement is the case in which child is overriding inherited
The method from parent with a functionality related to parent method functionality, sign of this process is calling generally super.method() in the body of child method.
替代品
1. **Replacement** is the case in which child class is overriding
父类继承的方法,其行为(功能)与相应的父方法完全不同,并为这个过程做标志,不是在子方法体中调用super.method()。
细化
2. Refinement is the case in which child is overriding inherited
来自父的方法具有与父方法功能相关的功能,该过程的标志通常是在子方法的主体中调用 super.method() 。
回答by Priyabrata G
It is totally a wrong concept that a method can only be overloaded in the same class. Instead method can be overload in the subclass also.
一个方法只能在同一个类中重载是完全错误的概念。相反,方法也可以在子类中重载。
回答by Nidhi Arora
Overriding and Redefining (also known as hiding) are almost the some thing except:
覆盖和重新定义(也称为隐藏)几乎是一些事情,除了:
Overriding is for instance methods and Redefining or hiding is for Class methods. Redefining is only in case of Static methods as Static methods don't have a run time Polymorphism.
覆盖用于实例方法,而重新定义或隐藏用于类方法。重新定义仅适用于静态方法,因为静态方法没有运行时多态性。
Please refer to the below code for clarification:
请参阅以下代码进行说明:
class Foo {
public static void classMethod()
SOP("classMethod() in Foo");
}
public void instanceMethod()
{
SOP ("instanceMethod() in Foo");
}
}
class Bar extends Foo {
public static void classMethod()
{
SOP ("classMethod() in Bar");
}
public void instanceMethod() {
SOP ("instanceMethod() in Bar");
}
}
class Test {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
}
}
The Output for this code is: instanceMethod() in Bar classMethod() in Foo
这段代码的输出是:instanceMethod() in Bar classMethod() in Foo
Reason for this Output:
此输出的原因:
Since instanceMethod () is an instance method, in which Bar overrides the method from Foo, at run time the J.V.M. uses the actual class of the instance f to determine which method to run. Although f was declared as a Foo, the actual instance we created was a new Bar(). So at run time, the J.V.M. finds that f is a Bar instance, and so it calls instanceMethod () in Bar rather than the one in Foo.
由于 instanceMethod() 是一个实例方法,其中 Bar 覆盖了来自 Foo 的方法,因此在运行时 JVM 使用实例 f 的实际类来确定要运行的方法。尽管 f 被声明为 Foo,但我们创建的实际实例是一个 new Bar()。所以在运行时,JVM发现f是一个Bar实例,所以调用Bar中的instanceMethod()而不是Foo中的。
With classMethod () , as it's a class method, the compiler and J.V.M. don't expect to need an actual instance to invoke the method. And even if you provide one the J.V.M. will never look at it. The compiler will only look at the declared type of the reference, and use that declared type to determine, at compile time, which method to call. Since f is declared as type Foo, the compiler looks at f.classMethod() and decides it means Foo. class Method. It doesn't matter that the instance referred to by f is actually a Bar - for static methods, the compiler only uses the declared type of the reference.
使用 classMethod () ,因为它是一个类方法,编译器和 JVM 不需要实际实例来调用该方法。即使你提供了一个 JVM 也不会查看它。编译器只会查看引用的声明类型,并在编译时使用该声明类型来确定要调用哪个方法。由于 f 被声明为 Foo 类型,编译器查看 f.classMethod() 并确定它表示 Foo。类方法。f 引用的实例实际上是一个 Bar 并不重要——对于静态方法,编译器只使用引用的声明类型。
That's why I said in the beginning that a static method does not have run-time polymorphism.
这就是为什么我在开始时说静态方法没有运行时多态性。
回答by Mark Davis
Redefinition is valid for Java and it is for static methods. If you want to change what a static method does for your class you can redefine it. This is not effected by polymophism since that doesn't apply to static methods.
重定义对 Java 和静态方法有效。如果您想更改静态方法为您的类所做的事情,您可以重新定义它。这不受多态性的影响,因为它不适用于静态方法。
Using your example all calls to the class name or all sub classes will get the original doBattle( )
method. A specific call to FunkyBattler.doBattle()
will invoke the redefined static method in FunkyBattler
class.
使用您的示例,所有对类名或所有子类的调用都将获得原始doBattle( )
方法。特定调用FunkyBattler.doBattle()
将调用FunkyBattler
类中重新定义的静态方法。
回答by aakash asthana
You have method "doBattle"which is staticand in Java static methods cannot be overridden, This doesn't mean they can't be redefined in a subclass, Thus here you are redefining the method doBattlein FunkyBattlerclass a subclass of DonkeyBattler
你有方法 “doBattle”这是静态的,并在Java静态方法不能被重写,这并不意味着他们不能在子类中被重新定义,因此在这里你正在重新定义方法doBattle在FunkyBattler类的子类DonkeyBattler
回答by Zakaria Bouazza
public class Animal{
public static void doStuff(){
System.out.println("In animal");
}
}
public class Dog extends Animal {
//redifinition of the method, since it is... Static
static void doStuff() {
System.out.println("In Dog...");
}
public static void main(String[] args) {
Animal animal = new Dog();
animal.doStuff();
}
}
The output will be 'In animal' even if you called the static method of reference to Dog, you should deduce then that runtime polymorphism doesnt work as with OVERRIDING.
即使您调用了引用 Dog 的静态方法,输出也将是“在动物中”,您应该推断运行时多态性与 OVERRIDING 不同。
回答by Jyoti Prakash
Redefining and Overriding comes with in the same scenarios. Only difference is that if methods used are Static, its redefining.
Redefining 和 Overriding 出现在相同的场景中。唯一的区别是,如果使用的方法是静态的,则对其进行重新定义。
ex:
前任:
Overriding:
覆盖:
Class A{
public void show(){
SOP("class a");
}
}
Class B extends A{
public void show(){
SOP("class B");
}
}
Redefining:
重新定义:
Class A{
public static void show(){
SOP("class a");
}
}
Class B extends A{
public static void show(){
SOP("class B");
}
}
Note: Static methods looks as if they are over-rided but they are actually redefined.
注意:静态方法看起来好像被覆盖了,但实际上是重新定义的。
- Redefining is with Static Methods.
- Static methods are associated with Class and not with Object, so we do not override as per instance for run-time.
- In case of static we are just redefining the method.
- 重新定义是使用静态方法。
- 静态方法与 Class 相关联,而不与 Object 相关联,因此我们不会根据运行时的实例进行覆盖。
- 在静态的情况下,我们只是重新定义方法。