简体   繁体   English

如何连接 java 中的字段?

[英]How to connect fields in java?

In my project I ran into a situation that I could not quickly google.在我的项目中,我遇到了无法快速搜索的情况。 I have several objects and write a value in the id field of each, when I change this id later, I need to update the value in the id fields of each object.我有几个对象并在每个对象的 id 字段中写入一个值,当我稍后更改此 id 时,我需要更新每个 object 的 id 字段中的值。 Example b = null; a = b; => a = null;示例b = null; a = b; => a = null; b = null; a = b; => a = null; after, for example, b = 3 , it is necessary to update the value of a = 3 without a = b again.例如,在b = 3之后,有必要在没有a = b的情况下再次更新a = 3的值。 I will try to explain in more detail.我将尝试更详细地解释。

class Student { private Long id; } 
class Class { private Long id; } 
Student s = new Student (); 
Class c = new Class (); 
c.getid = null; 
s.setId (c.getId); => s.getId == null;

after some operation c.getId == 1;经过一些操作c.getId == 1; but s.getId == null how i can do s.getId == 1 without s.setId (c.getId) ?但是s.getId == null如果没有s.setId (c.getId)我怎么能做s.getId == 1

Is this possible?这可能吗?

b = 3, it is necessary to update the value of a = 3 without a = b again. b = 3,需要在没有a = b的情况下再次更新a = 3的值。 Is this possible?这可能吗?

If fields a and b are of primitive data type then no.如果字段ab是原始数据类型,则不是。 You would need to update both a and b .您需要同时更新ab

If a and b are instance on an Object then yes, since a and b share the same memory reference.如果ab是 Object 上的实例,那么是的,因为ab共享相同的 memory 引用。 So if your fields are primitive data types you can wrap them inside a Custom Object with proper a set/get method and you are good to go.因此,如果您的字段是原始数据类型,您可以使用适当的 set/get 方法将它们包装在自定义 Object 中,并且您对 go 很好。

If those fields can be read/write concurrently by multithreads you may need to also ensure mutual exclusion when accessing them.如果这些字段可以由多线程同时读取/写入,您可能还需要确保在访问它们时互斥。

As I suspected, your question is in fact an XY Problem where you are barking up the wrong tree.正如我所怀疑的那样,您的问题实际上是一个XY 问题,您在错误的树上吠叫。 The solution is not to change the Student's ID, but rather to pass references where they are needed.解决方案不是更改学生 ID,而是在需要的地方传递参考。 Here, I am suspecting that id represents the id of a class, and so the best solution is to give the Class class an ArrayList<Student> field (since it will hold multiple Students), and to give your Student an ArrayList<Class> field (since a Student can attend multiple classes).在这里,我怀疑id代表 class 的id ,所以最好的解决方案是给 Class class 一个ArrayList<Student> ArrayList<Class> (因为它将给你的 Student Array 和 List 多个字段)字段(因为学生可以参加多个课程)。

Then, when a Student registers for a class (via some method), add that Class to its classes list, and add the Student to the Class's students list.然后,当学生注册 class(通过某种方法)时,将 Class 添加到其班级列表中,并将学生添加到班级的学生列表中。

eg,例如,

public class Student {
    private int id;
    private List<Class> classes = new ArrayList<>();
    
    // constructor, getters, setters
    
    public void addClass(Class class) {
        classes.add(class);
    }
    
    // also removeClass method, a getClasses method...
}
public class Class {
    private int id;
    private List<Student> students = new ArrayList<>();
    
    // constructor, getters, setters
    
    public void addStudent(Student student) {
        students.add(student);
    }
    
    // also removeStudent method, a getStudents method...
}

thanks for your good explanation of question, but that would be better if you told us why.感谢您对问题的良好解释,但如果您告诉我们原因会更好。 buy the way, I think this is a XY problem, the more important question is "why do you need this"?买的方式,我认为这是一个XY问题,更重要的问题是“你为什么需要这个”?

let's speak about your question, there are few options that you should consider.让我们谈谈您的问题,您应该考虑的选择很少。

  1. type of a and b is primitive since primitives have their own memory (not pointing to somewhere else), like C language, after the assignment nothing connects a and b and they can (and will) change separately. ab的类型是原始的,因为原语有自己的 memory(不指向其他地方),就像 C 语言一样,在分配之后没有任何东西连接ab并且它们可以(并且将)单独更改。 for example例如
int a, b;
a = 3;
b = a;

a = 5;
// b is still 3
  1. a and b are references and you want them to point to same object. ab是引用,您希望它们指向相同的 object。 Objects are in the heap and since there is no assignment overload (like we have in cpp ), after the assignment a and b are pointing to exact same object so each change in the object that a is pointing to, will seen from b.对象在堆中,并且由于没有赋值重载(就像我们在 cpp 中一样),在赋值后ab指向完全相同的 object 因此 a 指向的 object 中的每次更改都将从 b 中看到。 BUT REMEMBER this connection is unless a and b is pointing to same object and manipulate object state with .但请记住,除非 a 和 b 指向相同的 object 并使用. ; ;
class Student {
    int id;
}

class Demo {
    public static void main(String[] args) {
        Student a, b;
        a = new Student();
        a.id = 3;

        b = a;
        // they are now pointing to same object
        a.id = 5;
        // b.id is 5 too, basically same int

        b = null;
        // now they are disconnect
        // or maybe this: b = new Student()

    }
}

I think this could help and answer your question, but let's discuss about your code snippet too.我认为这可以帮助并回答您的问题,但让我们也讨论一下您的代码片段。

  1. if you want references themselves change.如果您希望引用本身发生变化。 for example:例如:
String a, b;
a = "some string"
b = a;

a = null;
// b  should be null?

as far as i know, there isn't something in java that provide this for you, no operator overloading or preprocessor that can help.据我所知,java 中没有任何东西可以为您提供此功能,没有运算符重载或预处理器可以提供帮助。

but if you insist I can think of some hacks .但如果你坚持我可以想到一些黑客

  • use C or any other customized preprocessor and define some macro to do this.使用 C 或任何其他自定义预处理器并定义一些宏来执行此操作。 for example rename all b variables with a before compilation with java compiler.例如,在使用 java 编译器编译之前,使用a重命名所有b变量。 check this 检查这个
  • define a singleton object (or just a dummy class) and a , b keep references to that object, this is actually combination of case 2 and 3;定义一个singleton object(或只是一个虚拟类)和ab保留对object的引用,这实际上是案例2和3的组合;

with static class:与 static class:

class Global {
    static Object reference;
}

class Demo {
    public static void main(String[] args) {
        Global a = null, b = null;
        a.reference  = "some string value";
        b.reference = null;

        // a.reference is null

    }
}

and with singleton class:并使用 singleton class:

class Singleton {
    private static final Singleton instance = new Singleton();
    public static Singleton getInstance() {
        return Singleton.instance;
    }
    private Singleton() {}

    Object ref;
}

class Demo {
    public static void main(String[] args) {
        Singleton a =  Singleton.getInstance();
        Singleton b = a;

        a.ref = "some string value";
        b.ref = null;

        // a.ref is null
        System.out.println(a.ref);
    }
}

(yes I know that isn't same thing that you wanted but java is simple language in syntax level) (是的,我知道这与您想要的不同,但 java 是语法级别的简单语言)

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

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