java 静态方法中的继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4987127/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:57:34  来源:igfitidea点击:

Inheritance in Static Methods

javainheritancestatic-methods

提问by Swaranga Sarma

Why does the below code print "Main"?

为什么下面的代码打印“Main”?

public class Main
{
    public static void method()
    {
        System.out.println("Main");
    }

    public static void main(String[] args)
    {
        Main m = new SubMain();
        m.method();
    }
}

class SubMain extends Main
{
    public static void method()
    {
        System.out.println("SubMain");
    }
}

At runtime, mis pointing to an instance of Submain, so it should conceptually print "SubMain".

在运行时,m指向 的一个实例Submain,因此它应该在概念上打印“SubMain”。

回答by porges

Static methods are resolved on the compile-time type of the variable. mis of type Main, so the method in Mainis called.

静态方法在变量的编译时类型上解析。m是 type Main,所以Main调用in 的方法。

If you change it to SubMain m ..., then the method on SubMainwill be called.

如果将其更改为SubMain m ...,则将SubMain调用on 方法。

回答by AlexR

It is because static methods are not polymorphic. Moreover static method should be invoked not by object but using the class, i.e. Main.method()or SubMain.method(). When you are calling m.method()java actually calls Main.method()because m is of type Main.

这是因为静态方法不是多态的。此外,静态方法不应由对象调用,而应使用类调用,即Main.method()SubMain.method()。当您调用m.method()java 时实际上会调用,Main.method()因为 m 是 Main 类型。

If you want to enjoy polymorphism do not use static methods.

如果您想享受多态性,请不要使用静态方法。

回答by developmentalinsanity

Eclipse gives me this sort of warning when I try to do this sort of thing:

当我尝试执行此类操作时,Eclipse 会向我发出此类警告:

The static method XXX() from the type XXX should be accessed in a static way

类型 XXX 的静态方法 XXX() 应该以静态方式访问

Static methods do not take part in inheritance. The variable is of type Main, so the compiler resolved your function call to Main.method().

静态方法不参与继承。该变量的类型为Main,因此编译器将您的函数调用解析为Main.method().

For added fun, try setting mto null.

为了增加乐趣,请尝试设置mnull

回答by mmccomb

Java performs early binding for static methods, unlike instance methods which are dynamically bound.

与动态绑定的实例方法不同,Java 对静态方法执行早期绑定。

Because your object variable is of type Main the call is bound to the superclass implementation at compile time.

因为您的对象变量是 Main 类型,所以调用在编译时绑定到超类实现。

A good explanation is available here.

这里有一个很好的解释。

回答by Anoop Singhal

static methods are statically binded with their class name because m is type of Main class then after compilation it would look like as following Main.method(); after compilation of your class run the following command javap -c Main u can see the jvm assembly code for Main class and u would see following m.method //invoke static invoke static ,invoke special tells that static binding invoke special,invoke interface tells that dynamic binding

静态方法与它们的类名静态绑定,因为 m 是 Main 类的类型,然后在编译后它看起来像下面的 Main.method(); 在你的类编译后运行以下命令 javap -c Main 你可以看到 Main 类的 jvm 汇编代码,你会看到以下 m.method //invoke static invoke static ,invoke special 告诉静态绑定调用特殊,调用接口告诉那个动态绑定