我想做以下事情:

MyObject obj1 = new MyObject();
MyObject obj2 = new MyObject();
MyObject obj3 = new MyObject();
MyObject obj4 = new MyObject();

values[obj1][obj2] = 1;
values[obj2][obj4] = 2;
values[obj1][obj4] = 3;

int someValue = values[obj1][obj2];
int someOtherValue = values[obj2][obj4];
int anotherValue = values[obj1][obj4];


如何在Java中使用两个对象作为键来创建这些getter和setter方法?

最佳答案

您可以使用另一个类返回“两个对象”。可能是您可以解决此问题

    final class MyResultObject {
            private final MyObject firstObject;
            private final MyObject secondObject;
        private final int key;
            public MyResult(MyObject firstObject, MyObject secondObject ,int key) {
                this.firstObject = firstObject;
                this.secondObject = secondObject;
                this.key=key;
            }


    public int getKey() {
                return key;
            }
    Public int getKeyValue(MyObject Object1, MyObject Object2){

}
        }

       //and then you can workaround this
        public static void main(String[] args) {
            MyResultObject resultOb = new MyResultObject(obj1,obj2,1);
            System.out.println(resultOb.getFirstObject() + resultOb.getSecondOBject());
        }

关于java - 如何从Java中的两个对象检索值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29874215/

10-14 08:05