Java中的继承如何工作? [英] How does inheritance in Java work?

查看:102
本文介绍了Java中的继承如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有下一个课程:

class Super {
    void foo() {
        System.out.println("Super");
    }
}

class Sub extends Super {
    void foo() {
        super.foo();
        System.out.println("Sub");
    }
}

public class Clazz {
    public static void main(String[] args) {
        new Sub().foo();
    }
}

输出为:


超级

Super

Sub



问题:



super 有什么用?它是父类的对象,哪个孩子保持为字段?

Questions:

What does super present? Is it object of parent class, which child keeps as field?


  • 如果是,抽象类的继承如何工作?你不能创建抽象类的实例。

  • 如果不是,那么被覆盖的方法在哪里?

我试过谷歌,但我找到的只是关于如何继承类等的常见信息。

I tried to Google, but all I found is common information about how to inherit classes and so on.

你还在告诉我显而易见的事情。也许我的问题有点误导,但我会尝试改写它:

You are still telling me obvious things. Maybe my question was little misleading, but I'll try to rephrase it:


  • 当我们用调用方法时超级,你说,我们正在访问父方法。但是如何在没有父对象的情况下调用此方法?

  • super 相同这个是对具体对象的引用,如你所知。

  • When we are calling method with super, you say, we are accessing parent's method. But how can we call this method without parent's object?
  • Is super same as this? this is a reference to concrete object, as you know.

推荐答案

子类不维护任何表示其父级的特殊字段。您可能正在考虑内部类的内容, 维护对其外部类的引用。这是一种特殊情况,并不代表超级子类如何相互关联。

The child class does not maintain any special field representing its parent. You may be thinking something along the lines of inner classes, which do maintain a reference to their outer class. This is a special case and does not represent how super-sub classes relate to each other.

在内部,JVM维护一个'方法表',将其加载的每个类与该类可用的方法相关联。 JVM还知道它加载的所有类之间的关系,包括超子关系。

Internally, the JVM maintains a 'method table', associating each class its loaded with the methods available for that class. The JVM also knows about the relationships between all classes its loaded, including super-sub relationships.

当你调用 super 函数,JVM实际上做了两件事:

When you invoke a super function, the JVM actually does a couple of things:


  • 确定从<调用方法的类的父级/ li>
  • 确定将被调用的父方法

  • 使用特殊指令调用方法( invokespecial

  • Determines the parent of the class that the method is is being called from
  • Determines the method on the parent that will be called
  • Invokes the method, with a special instruction (invokespecial)

如果您要检查 Sub class,你会看到类似这样的 foo 方法:

If you were to examine the class file for your Sub class, you would see something like this for the foo method:

void foo();
    flags: 
    Code:
        stack=2, locals=1, args_size=1
            0: aload_0       
            1: invokespecial #2        // Method Super.foo:()V
            4: getstatic     #3        // Field java/lang/System.out:Ljava/io/PrintStream;
            7: ldc           #4        // String Sub
            9: invokevirtual #5        // Method java/io/PrintStream.println:(Ljava/lang/String;)V

列表中的第1行显示调用超类方法的特殊指令。

一个很好的阅读来源是 Java虚拟机规范,特别是第2.11.8节

A good source of reading would be the Java Virtual Machine Specification, particularly Section 2.11.8.

这篇关于Java中的继承如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆