Java中隐藏的方法是什么?甚至 JavaDoc 的解释也令人困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16313649/
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 is method hiding in Java? Even the JavaDoc explanation is confusing
提问by JATMON
Javadocsays:
Javadoc说:
the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass.
被调用的隐藏方法的版本是超类中的版本,而被调用的覆盖方法的版本是子类中的版本。
doesn't ring a bell to me. Any clear example showing the meaning of this will be highly appreciated.
不给我敲响警钟。任何显示此含义的清晰示例都将受到高度赞赏。
采纳答案by JB Nizet
public class Animal {
public static void foo() {
System.out.println("Animal");
}
}
public class Cat extends Animal {
public static void foo() { // hides Animal.foo()
System.out.println("Cat");
}
}
Here, Cat.foo()
is said to hide Animal.foo()
. Hiding does not work like overriding, because static methods are not polymorphic. So the following will happen:
这里,Cat.foo()
据说是躲Animal.foo()
。隐藏不像覆盖那样工作,因为静态方法不是多态的。所以会发生以下情况:
Animal.foo(); // prints Animal
Cat.foo(); // prints Cat
Animal a = new Animal();
Animal b = new Cat();
Cat c = new Cat();
Animal d = null;
a.foo(); // should not be done. Prints Animal because the declared type of a is Animal
b.foo(); // should not be done. Prints Animal because the declared type of b is Animal
c.foo(); // should not be done. Prints Cat because the declared type of c is Cat
d.foo(); // should not be done. Prints Animal because the declared type of d is Animal
Calling static methods on instances rather than classes is a very bad practice, and should never be done.
在实例而不是类上调用静态方法是一种非常糟糕的做法,永远不应该这样做。
Compare this with instance methods, which are polymorphic and are thus overridden. The method called depends on the concrete, runtime type of the object:
将此与实例方法进行比较,实例方法是多态的,因此会被覆盖。调用的方法取决于对象的具体运行时类型:
public class Animal {
public void foo() {
System.out.println("Animal");
}
}
public class Cat extends Animal {
public void foo() { // overrides Animal.foo()
System.out.println("Cat");
}
}
Then the following will happen:
然后会发生以下情况:
Animal a = new Animal();
Animal b = new Cat();
Animal c = new Cat();
Animal d = null;
a.foo(); // prints Animal
b.foo(); // prints Cat
c.foo(); // prints Cat
d.foo(): // throws NullPointerException
回答by Jeremy Unruh
For example you can override instance methods in a super class but not static.
例如,您可以覆盖超类中的实例方法,但不能覆盖静态方法。
Hiding is Parent class has a static method named Foo and the sub class also has a static method called Foo.
Hiding is 父类有一个名为 Foo 的静态方法,子类也有一个名为 Foo 的静态方法。
Another scenario is the parent has a static method named Cat and the sub class has an instance method named Cat. (static and instance with the same signature can't intermix).
另一种情况是父类有一个名为 Cat 的静态方法,而子类有一个名为 Cat 的实例方法。(静态和具有相同签名的实例不能混合)。
public class Animal {
public static String getCat() { return "Cat"; }
public boolean isAnimal() { return true; }
}
public class Dog extends Animal {
// Method hiding
public static String getCat() { }
// Not method hiding
@Override
public boolean isAnimal() { return false; }
}
回答by Suresh Atta
To overwritea method means that whenever the method is called on an object of the derived class, the new implementation will be called.
到覆盖的方法的装置,每当该方法被称为派生类的物体上时,新的实现将被调用。
To hidea method means that an unqualified call to that name in the scope of this class (i.e. in the body of any of its methods, or when qualified with the name of this class) will now call a completely different function, requiring a qualification to access the static method of the same name from the parent class.
到隐藏的方法的装置,在这个类的(即,在其任何方法,或当合格与这个类的名称的团体)的范围,以该名称的不合格呼叫现在将调用完全不同的功能,需要鉴定从父类访问同名静态方法。
More description Java Inheritance: Overwritten or hidden methods
更多描述Java 继承:覆盖或隐藏的方法
回答by NINCOMPOOP
If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.
如果子类定义了与超类中的类方法具有相同签名的类方法,则子类中的方法将隐藏超类中的方法。
Hidden methods are in Static context, I believe. Static methods are not overridden, per se, because the resolution of method calls done by the compiler at the compile time itself. So, if you define a static method in the base class with the same signature as that one present in the parent class, then the method in the subclass hides the method inherited from super class.
我相信隐藏方法在静态上下文中。静态方法本身不会被覆盖,因为方法调用的解析由编译器在编译时本身完成。因此,如果您在基类中定义一个静态方法,其签名与父类中的静态方法相同,那么子类中的方法会隐藏从超类继承的方法。
class Foo {
public static void method() {
System.out.println("in Foo");
}
}
class Bar extends Foo {
public static void method() {
System.out.println("in Bar");
}
}
回答by Vishal K
First of all What is meant by method Hiding?
首先,隐藏方法是什么意思?
Method hiding means subclass has defined a class methodwith the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass. It signifies that : The version of a method that is executed will NOTbe determined by the object that is used to invoke it. In fact it will be determined by the type of reference variable used to invoke the method.
方法隐藏意味着子类已经定义了一个与超类中的类方法具有相同签名的类方法。在这种情况下,超类的方法被子类隐藏。它表示:执行的方法的版本不会由用于调用它的对象决定。事实上,它将由用于调用该方法的引用变量的类型决定。
What is meant by method overriding?
方法覆盖是什么意思?
Method overriding means subclass had defined an instance methodwith the same signature and return type( including covariant type) as the instance method in superclass. In that case method of superclass is overridden(replaced) by the subclass. It signifies that: The version of method that is executed will bedetermined by the object that is used to invoke it. It will not be determined by the type of reference variable used to invoke the method.
方法覆盖意味着子类定义了一个与超类中的实例方法具有相同签名和返回类型(包括协变类型)的实例方法。在这种情况下,超类的方法被子类覆盖(替换)。它表示:执行的方法的版本将由用于调用它的对象决定。它不会由用于调用该方法的引用变量的类型决定。
Why can't static methods be overridden?
为什么不能覆盖静态方法?
Because, static methods are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object.
因为,静态方法是基于它们被调用的类静态地(即在编译时)解析的,而不是像实例方法那样动态解析,实例方法是基于对象的运行时类型以多态方式解析的。
How should static methods be accessed?
应该如何访问静态方法?
Static methods should be accessed in static way. i.e. by the name of class itself rather than using an instance.
静态方法应该以静态方式访问。即通过类本身的名称而不是使用实例。
Here is the short Demo for method overriding and hiding:
这是方法覆盖和隐藏的简短演示:
class Super
{
public static void foo(){System.out.println("I am foo in Super");}
public void bar(){System.out.println("I am bar in Super");}
}
class Child extends Super
{
public static void foo(){System.out.println("I am foo in Child");}//Hiding
public void bar(){System.out.println("I am bar in Child");}//Overriding
public static void main(String[] args)
{
Super sup = new Child();//Child object is reference by the variable of type Super
Child child = new Child();//Child object is referenced by the variable of type Child
sup.foo();//It will call the method of Super.
child.foo();//It will call the method of Child.
sup.bar();//It will call the method of Child.
child.bar();//It will call the method of Child again.
}
}
Output is
输出是
I am foo in Super
I am foo in Child
I am bar in Child
I am bar in Child
Clearly, as specified, since foo
is the class method so the version of foo
invoked will be determined by the type of reference variable (i.e Super or Child) referencing the object of Child
. If it is referenced by Super
variable then foo
of Super
is called. And if it is referenced by Child
variable then foo
of Child
is called.
Whereas,
Since bar
is the instance method so the version of bar
invoked is solely determined by the object(i.e Child
) that is used to invoke it. No matter via which reference variable (Super
or Child
) it is called , the method which is going to be called is always of Child
.
显然,正如指定的那样,由于foo
是类方法,因此foo
被调用的版本将由引用 的对象的引用变量(即 Super 或 Child)的类型确定Child
。如果它被Super
变量引用foo
,Super
则调用of 。如果它被Child
变量引用foo
,Child
则调用of 。
而,
Sincebar
是实例方法,因此bar
被调用的版本完全由Child
用于调用它的对象(即)决定。无论通过哪个引用变量(Super
或Child
)调用它,将要调用的方法始终是 of Child
。
回答by Sindhuja
class P
{
public static void m1()
{
System.out.println("Parent");
}
}
class C extends P
{
public static void m1()
{
System.out.println("Child");
}
}
class Test{
public static void main(String args[])
{
Parent p=new Parent();//Parent
Child c=new Child(); //Child
Parent p=new Child(); //Parent
}
}
If the both parent and child class method are static the compiler is responsible for method resolution based on reference type
class Parent
{
public void m1()
{
System.out.println("Parent");
}}
class Child extends Parent
{
public void m1()
{
System.out.println("Child")
}
}
class Test
{
public static void main(String args[])
{
Parent p=new Parent(); //Parent
Child c=new Child(); //Child
Parent p=new Child(); //Child
}
}
If both method are not static jvm is responsible for method resolution based on run time object
回答by Suresh Kumar Msk
When super/parent class and sub/child class contains same static method including same parameters and signature. The method in the super class will be hidden by the method in sub class. This is known as method hiding.
当超/父类和子/子类包含相同的静态方法时,包括相同的参数和签名。超类中的方法会被子类中的方法隐藏。这称为方法隐藏。
Example:1
示例:1
class Demo{
public static void staticMethod() {
System.out.println("super class - staticMethod");
}
}
public class Sample extends Demo {
public static void main(String args[] ) {
Sample.staticMethod(); // super class - staticMethod
}
}
Example:2 - Method Hiding
示例:2 - 方法隐藏
class Demo{
public static void staticMethod() {
System.out.println("super class - staticMethod");
}
}
public class Sample extends Demo {
public static void staticMethod() {
System.out.println("sub class - staticMethod");
}
public static void main(String args[] ) {
Sample.staticMethod(); // sub class - staticMethod
}
}