用Java如何实现super? [英] How super is implemented in Java?

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

问题描述

实际上在哪里定义了超级"? [当我们使用super.someMethod()时].是否将其定义为java.lang.Object类或java.lang.Class类中的字段?

Where the "super" is actually defined? [When we're using super.someMethod()]. Is it defined as a field in java.lang.Object class or java.lang.Class class?

当我们从子类中调用时,super包含对其超类的引用.以同样的方式,超类中的super本身也引用了其超类[以这种方式直到java.lang.Object].那么,java如何将超类引用注入到"super"字段中,从而使我们能够调用超类方法?

When we're calling from a subclass, super contains the reference to it's superclass.In the same manner the super in superclass itself has reference to it's superclass [In this way upto java.lang.Object]. So, how java injects the superclass references to the "super" field which enables us to call superclass methods ?

有没有类似的内幕实现?

Is there any under the hood implementaations like the following:

    Class current = this.getClass();
    Class parent = current.getSuperclass();
    System.out.println("name is =" + parent);

推荐答案

super是特定于对象引用的关键字,从调用对象开始,直到找到具有匹配方法签名或匹配方法的祖先对象,该对象层级才升序.属性/字段.请参考 Java文档.

super is an object-reference specific keyword that ascends the object hierarchy beginning from the calling object until it finds an ancestor object that has a matching method signature or property/field. Refer to Java documentation.

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

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