静态方法和多态性

发布于 2025-01-29 03:43:40 字数 473 浏览 2 评论 0原文

我有一个简单的问题,我无法找到一个很好的答案。为什么以下Java程序显示20?如果可能的话,我更喜欢详细的答复。

class Something{
    public int x;
    public Something(){
        x=aMethod();
    }
    public static int aMethod(){
        return 20;
    }
}
class SomethingElse extends Something{
    public static int aMethod(){
        return 40;
    }
    public static void main(String[] args){
        SomethingElse m;
        m=new SomethingElse();
        System.out.println(m.x);
    }
}

I have a simple question that I just can't figure out a good answer for. Why does the following Java program display 20? I would prefer a detailed response if possible please.

class Something{
    public int x;
    public Something(){
        x=aMethod();
    }
    public static int aMethod(){
        return 20;
    }
}
class SomethingElse extends Something{
    public static int aMethod(){
        return 40;
    }
    public static void main(String[] args){
        SomethingElse m;
        m=new SomethingElse();
        System.out.println(m.x);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2025-02-05 03:43:40

因为多态性仅适用于实例方法。

static方法amethod在此调用

public Something(){
    x=aMethod();
}

amethod> something中声明。

Because polymorphism only applies to instance methods.

The static method aMethod invoked here

public Something(){
    x=aMethod();
}

refers to the aMethod declared in Something.

醉南桥 2025-02-05 03:43:40

静态方法的继承的起作用与非静态方法不同。特别是,超类静态方法并未被子类覆盖。静态方法调用的结果取决于它被调用的对象类。变量x是在某物对象创建期间创建的,因此,静态方法被调用以确定其值。

考虑以下代码:

public static void main(String[] args){
  SomethingElse se = new SomethingElse();
  Something     sg = se;
  System.out.println(se.aMethod());
  System.out.println(sg.aMethod());
}

它将正确打印40、20,因为每个对象类都调用自己的静态方法。 java文档在隐藏静态方法部分中描述了这种行为。

The inheritance for static methods works differently then non-static one. In particular the superclass static method are NOT overridden by the subclass. The result of the static method call depends on the object class it is invoke on. Variable x is created during the Something object creation, and therefore that class (Something) static method is called to determine its value.

Consider following code:

public static void main(String[] args){
  SomethingElse se = new SomethingElse();
  Something     sg = se;
  System.out.println(se.aMethod());
  System.out.println(sg.aMethod());
}

It will correctly print the 40, 20 as each object class invokes its own static method. Java documentation describes this behavior in the hiding static methods part.

月下客 2025-02-05 03:43:40

因为在类的类中声明了int x。当您制作something emavelse对象时,您首先制作themess对象(必须设置x,然后使用amethod()从<<代码>某物而不是something oferes(因为您正在创建themings))。这是因为Amethod()是静态的,并且多态性对于静态方法不起作用。然后,当您从M中打印X时,您打印20,因为您从未更改x的值。

Because int x is declared in the class Something. When you make the SomethingElse object, you first make a Something object (which has to set x, and it uses the aMethod() from Something instead of SomethingElse (Because you are creating a Something)). This is because aMethod() is static, and polymorphism doesn't work for static methods. Then, when you print the x from m, you print 20 since you never changed the value of x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文