简体   繁体   English

java子类是否使用相同的超类字段对象

[英]Does java subclass use the same superclass field object

I am currently learning Java. 我目前正在学习Java。 Look at the code below: 看下面的代码:

package classtest1;

class ClassSuper
{
    public Object myObject = new Object();

    public ClassSuper(){}
}

public class ClassTest1 extends ClassSuper
{
    public ClassTest1()
    {
        System.out.println("this.myObject.equals(super.myObject) return: " + this.myObject.equals(super.myObject));
        System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));
    }

    public static void main(String[] args)
    {
        ClassTest1 myClassTest1 = new ClassTest1();
    }

}

the output is below: 输出如下:

run:
this.myObject.equals(super.myObject) return: true
false
BUILD SUCCESSFUL (total time: 0 seconds)

My question is that, why equals and "==" are not the same? 我的问题是,为什么等于和“==”不一样? Why output false when using "==". 使用“==”时为什么输出false。 Will Subclass create a new copy myObject in memory? Subclass会在内存中创建一个新的副本myObject吗?

Will Subclass create a new copy myObject in memory? Subclass会在内存中创建一个新的副本myObject吗?

No. You are simply not comparing the Objects you think you are comparing. 不。您只是不比较您认为比较的对象。

System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));

compares the String "this.myObject == (super.myObject) return: " + this.myObject to (super.myObject) and returns false . String "this.myObject == (super.myObject) return: " + this.myObject(super.myObject)并返回false

When the argument passed to System.out.println is evaluated, it is evaluated from left to right. 当计算传递给System.out.println的参数时,将从左到右进行计算。 First this.myObject.toString() is concatenated to "this.myObject == (super.myObject) return: " , and then the resulting String is compared to (super.myObject) with the == operator. 首先将this.myObject.toString()连接到"this.myObject == (super.myObject) return: " ,然后将生成的String与带有==运算符的(super.myObject)进行比较。

If you wrap the comparison with parentheses : 如果用括号包装比较:

System.out.println("this.myObject == (super.myObject) return: " + (this.myObject == super.myObject));

you'll get the comparison you intended which will return true , since this.myObject and super.myObject refer to the same Object . 你将获得你想要的比较将返回true ,因为this.myObjectsuper.myObject引用相同的Object

System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));
Actually this is comparing strings. 实际上这是比较字符串。 First String is 第一个字符串是
"this.myObject == (super.myObject) return: " + this.myObject
Second String is 第二个字符串是
(super.myObject)
If you want to compare objects use small bracket as 如果要比较对象,请使用小括号作为
(this.obj == (super.obj))

You are confused with the output produced so below are few points you need to focus on. 您对产生的输出感到困惑,因此下面是您需要关注的几点。

Operator Preferences : Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. 运算符首选项 :Java具有明确定义的规则,用于指定表达式具有多个运算符时计算表达式中运算符的顺序。

For your better understanding check the try executing the below code. 为了更好地理解,请检查执行以下代码的尝试。

public class ClassTest1 extends ClassSuper
{
    public ClassTest1()
    {
        System.out.println("this.myObject.equals(super.myObject) return: " + (this.myObject.equals(super.myObject)));
        System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));
        System.out.println("this.myObject == (super.myObject) return: " + (this.myObject == (super.myObject)));
    }

   ...

}

Console of your program with above changes. 具有上述更改的程序控制台。

this.myObject.equals(super.myObject) return: true
false
this.myObject == (super.myObject) return: true

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM