继承混乱-"this"的值在构造函数中打印时 [英] confusion in inheritance - value of "this" when printed in constructor

查看:78
本文介绍了继承混乱-"this"的值在构造函数中打印时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.

class Test {
    int i = 0;

    Test() {
        System.out.println(this);
        System.out.println(this.i);
    }

}

public class Demo extends Test {
    int i = 10;

    Demo() {
        super();
        System.out.println("calling super");
        System.out.println(this);
        System.out.println(this.i);    
    }

    public static void main(String[] args) throws IOException {    
        Demo d = new Demo();    
    }
}

O/P : Demo@2e6e1408
0
calling super
Demo@2e6e1408
10

当我执行程序并打印"this"的值时,在超类构造函数和子类构造函数中,此(地址位置)的值都显示为childClassName @ someValue ..我的问题是,当我在超级类中打印"this"的值时,为什么没有得到Test的值,即Test @ someVal(超级类).ASAIK,超级类在内存中也会有一个位置/位置,所以,为什么我在第一个SOP中没有得到Test @ someValue ...

When I execute the program and print the value of "this", in both super class constructor as well as in child class constructor, the value of this (address location) is displayed as childClassName@someValue .. My question is, why dont I get the value of Test i.e, Test@someVal (Super class) when I print value of "this" in the super class.. ASAIK, Super class will also have a place/location in memory, so, why am I not getting Test@someValue in the first SOP...

PS:我知道变量是根据引用类型(LHS)引用的,而方法是根据对象类型(RHS)调用的..

PS : I know variables are referenced based on the reference type (LHS) and methods are called based on the object type (RHS)..

推荐答案

当我执行程序并在超级类构造函数和子类构造函数中打印"this"的值时,this(地址位置)的值...

When I execute the program and print the value of "this", in both super class constructor as well as in child class constructor, the value of this (address location)...

默认值为Object#toStringSystem.out.println(this)的输出为不是内存中实例的地址.它只是类的名称和实例的哈希码,仅此而已.从文档:

The output of System.out.println(this) with the default Object#toString is not the address of the instance in memory. It's just the name of the class and the instance's hash code, nothing more. From the documentation:

Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号符'@'和该对象的哈希码的无符号十六进制表示形式.换句话说,此方法返回的字符串等于:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

hashCode文档

通常通过将对象的内部地址转换为整数来实现...

This is typically implemented by converting the internal address of the object into an integer...

但它也说

...但是Java TM 编程语言不需要此实现技术.

...but this implementation technique is not required by the JavaTM programming language.

...并且当然,JVM可以根据需要自由地在内存中移动实例,但不允许更改hashCode.

...and of course the JVM is free to move instances around in memory as necessary, but isn't allowed to change the hashCode.

...为什么当我打印"this"值时为什么没有得到Test的值,即Test @ someVal(超级类)?在超级班上.

...why dont I get the value of Test i.e, Test@someVal (Super class) when I print value of "this" in the super class.

一个实例.该实例属于子类.当您执行System.out.println(this)时,无论在基类还是子类中执行此操作都无关紧要,它仍然是您使用的同一对象实例.一个对象具有功能,它是从子类获得的,还具有从超类继承的功能,但是没有两个单独的实例.有一个实例具有一组组合的功能. super不是对象引用,尽管看起来有点像.这是一种语法机制,用于专门要求编译器使用实例从超类继承的功能,而不是实例的等效功能(如果它们不同).

There is one instance. That instance is of the subclass. When you do System.out.println(this), it doesn't matter whether you do that in the base class or the subclass, it's still the same object instance you're using. That one object has features it gets from the subclass and also features it inherits from the superclass, but there aren't two separate instances; there's one instance with a combined set of features. super isn't an object reference, although it looks a bit like one; it's a syntax mechanism for specifically asking the compiler to use a feature the instance inherits from the superclass rather than the equivalent feature of the instance (in case they're different).

这篇关于继承混乱-"this"的值在构造函数中打印时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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