如何访问类的特定实例的值?

问题描述 投票:-2回答:1

假设我们有这个课程:

public class Thing {
    public int myInt;

    public Thing(int x, int y){
    // some code here
    }
}

而这里的其他课程:

public class anotherClass{
    ArrayList<Thing> thingList = new ArrayList<Thing>();

    public static void main(String[] args) {
        Thing thing = new Thing(1,0);
        Thing thing = new Thing(0,1);

        thingList.add(thing);

        System.out.println(thingList); // this prints [Thing@538e82g2, Thing@9g3s1f53]
    }
}

我的问题是如何在实际工作代码中执行以下操作:

[email protected] = 10;
[email protected] = 15;

如果那是不可能的,最接近的工作是什么?

java class
1个回答
1
投票

不可能。看起来像9g3s1f53的东西是对象的哈希码,而不是指针/内存地址或任何类似的东西。特别是,哈希码(1)不是唯一的,并且(2)如果对象不是不可变的,则可以改变。

© www.soinside.com 2019 - 2024. All rights reserved.