简体   繁体   English

仍在尝试理解Java中的面向对象编程

[英]Still trying to understand object oriented programming in Java

Assuming there is a class named Hello, How would I declare variables named var1 and var2 to be references to objects in the class Hello? 假设有一个名为Hello的类,我如何将名为var1和var2的变量声明为对Hello类中对象的引用? I assumed it would just be Hello var1, var2; 我以为那只是Hello var1,var2;

Also to just construct an instance of the object of the class Hello using the default constructor would it just be Hello hello = new Hello(); 同样,要使用默认构造函数仅构造Hello类的对象的实例,也可以是Hello hello = new Hello();。

Finally my last question is if I were to instantiate an object of the class Hello using the default constructor and assign that object to the varaible named var1 it would just be Hello var1 = new Hellow();. 最后,我的最后一个问题是,如果要使用默认构造函数实例化Hello类的对象,并将该对象分配给名为var1的变量,则它将只是Hello var1 = new Hellow();。 How would I assign the reference to the object named var1 to the variable named var2 如何将对名为var1的对象的引用分配给名为var2的变量

And I know there is a term to describe the current state of the variables var1 and var2, but I cannot think of it 而且我知道有一个术语来描述变量var1和var2的当前状态,但是我想不到

Just given test example to illustrate the things 只是给出测试示例来说明事情

public class Hello {
    Hello var1, var2;
    public static void main(String[] args){
        Hello h1 = new Hello();
        h1.var1 = h1;
        System.out.println("h1.var1 ---- "+ h1.var1);

        Hello h2 = new Hello();
        h1.var2 = h2;
        System.out.println("h1.var2 ---- "+ h1.var2);

        h1.var2 = h1.var1;

        System.out.println("h1.var1 ---- "+ h1.var1);
        System.out.println("h1.var2 ---- "+ h1.var2);
    }
}


    Output :- 

    h1.var1 ---- Hello@19e0bfd
    h1.var2 ---- Hello@139a55
    h1.var1 ---- Hello@19e0bfd
    h1.var2 ---- Hello@19e0bfd

Assuming there is a class named Hello, How would I declare variables named var1 and var2 to be references to objects in the class Hello? 假设有一个名为Hello的类,我如何将名为var1和var2的变量声明为对Hello类中对象的引用? I assumed it would just be Hello var1, var2; 我以为那只是Hello var1,var2;

You can see there are two variables declared of same class which can be reference to Hello class instance. 您可以看到有两个声明为同一类的变量,它们可以引用Hello类实例。

Also to just construct an instance of the object of the class Hello using the default constructor would it just be Hello hello = new Hello(); 同样,要使用默认构造函数仅构造Hello类的对象的实例,也可以是Hello hello = new Hello();。

Yes there is default constructor associated with each class so you can do initialize class like this. 是的,每个类都有默认的构造函数,因此您可以像这样初始化类。

Finally my last question is if I were to instantiate an object of the class Hello using the default constructor and assign that object to the varaible named var1 it would just be Hello var1 = new Hellow();. 最后,我的最后一个问题是,如果要使用默认构造函数实例化Hello类的对象,并将该对象分配给名为var1的变量,则它将只是Hello var1 = new Hellow();。 How would I assign the reference to the object named var1 to the variable named var2 如何将对名为var1的对象的引用分配给名为var2的变量

As you see the output the hash code of var2 is same as var1 after h1.var2 = h1.var1 statement. 如您所见,在h1.var2 = h1.var1语句之后, var2的哈希码与var1相同。 This states that var2 previous reference is replaced by var1 reference. 这说明var2先前的引用已被var1引用所替代。 So here objects are not copied but references to objects are copied. 因此,这里不复制对象,而是复制对对象的引用。 Check the same hashcode of var1 and var2 . 检查var1var2的相同hashcode

Thats it!. 而已!。

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

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