Java 如何使对象引用到同一对象?
///(不同的文件,相同的目录) 我想要Java 如何使对象引用到同一对象?,java,Java,///(不同的文件,相同的目录) 我想要keith.jim=justin.jim和justin.jim=keith.jim。这样,如果我做了keith.jim.SetNum(34),那么keith和justin的jim.j都将是34。怎么做?这个想法将在一段更大的代码中实现 另外,user.java和Number。java必须存在。不能更改上下文,我可以添加新方法,但不能在Number中更改上下文(例如,将j放入user而不是Number)。每个用户都必须有一个每个用户都要引用的数字对象。试试这
keith.jim=justin.jim
和justin.jim=keith.jim
。这样,如果我做了keith.jim.SetNum(34)
,那么keith
和justin的jim.j
都将是34
。怎么做?这个想法将在一段更大的代码中实现
另外,user.java
和Number
。java必须存在。不能更改上下文,我可以添加新方法,但不能在Number
中更改上下文(例如,将j放入user而不是Number)。每个用户都必须有一个每个用户都要引用的数字对象。试试这个
public class User {
private Number jim;
public User(){
this.jim = new Number();
}
public static void main(String []args){
User keith = new User();
User justin = new User();
??????????????????????????
}
}
试试这个
public class User {
private Number jim;
public User(){
this.jim = new Number();
}
public static void main(String []args){
User keith = new User();
User justin = new User();
??????????????????????????
}
}
试试这个
public class User {
private Number jim;
public User(){
this.jim = new Number();
}
public static void main(String []args){
User keith = new User();
User justin = new User();
??????????????????????????
}
}
试试这个
public class User {
private Number jim;
public User(){
this.jim = new Number();
}
public static void main(String []args){
User keith = new User();
User justin = new User();
??????????????????????????
}
}
其他人建议只创建一个User
对象。另一种选择是有两个User
对象,但对jim
字段的值具有相同的引用:
User keith = new User();
User justin = keith;
如果您有其他字段,其中keith
和justin
需要不同,但是jim
字段的值所引用的对象中的任何更改都必须被这两个字段看到,那么这将非常有用
这两个选项都是有效的,具体取决于您的需求
然而,有几点:
- 这意味着justin.jim的原始值是无意义的;您可以考虑添加一个<代码>用户< /COD>构造函数,使用该字段的初始值<代码>编号>代码>引用,允许您编写:
User keith = new User();
User justin = new User();
justin.jim = keith.jim;
- 像这样的共享可变状态可能变得难以推理,特别是在面对多个线程时
- 如注释中所述,您应该将
Number
类重命名为java.lang
(或者java.util
等,如果可能的话)
- 对于方法,您应该遵循Java命名约定,使用camelCase而不是PascalCase——我个人将它们称为
getValue
和setValue
其他人建议只创建一个
用户
对象。另一种选择是有两个User
对象,但对jim
字段的值具有相同的引用:
User keith = new User();
User justin = keith;
如果您有其他字段,其中keith
和justin
需要不同,但是jim
字段的值所引用的对象中的任何更改都必须被这两个字段看到,那么这将非常有用
这两个选项都是有效的,具体取决于您的需求
然而,有几点:
- 这意味着justin.jim的原始值是无意义的;您可以考虑添加一个<代码>用户< /COD>构造函数,使用该字段的初始值<代码>编号>代码>引用,允许您编写:
User keith = new User();
User justin = new User();
justin.jim = keith.jim;
- 像这样的共享可变状态可能变得难以推理,特别是在面对多个线程时
- 如注释中所述,您应该将
Number
类重命名为java.lang
(或者java.util
等,如果可能的话)
- 对于方法,您应该遵循Java命名约定,使用camelCase而不是PascalCase——我个人将它们称为
getValue
和setValue
其他人建议只创建一个
用户
对象。另一种选择是有两个User
对象,但对jim
字段的值具有相同的引用:
User keith = new User();
User justin = keith;
如果您有其他字段,其中keith
和justin
需要不同,但是jim
字段的值所引用的对象中的任何更改都必须被这两个字段看到,那么这将非常有用
这两个选项都是有效的,具体取决于您的需求
然而,有几点:
- 这意味着justin.jim的原始值是无意义的;您可以考虑添加一个<代码>用户< /COD>构造函数,使用该字段的初始值<代码>编号>代码>引用,允许您编写:
User keith = new User();
User justin = new User();
justin.jim = keith.jim;
- 像这样的共享可变状态可能变得难以推理,特别是在面对多个线程时
- 如注释中所述,您应该将
Number
类重命名为java.lang
(或者java.util
等,如果可能的话)
- 对于方法,您应该遵循Java命名约定,使用camelCase而不是PascalCase——我个人将它们称为
getValue
和setValue
其他人建议只创建一个
用户
对象。另一种选择是有两个User
对象,但对jim
字段的值具有相同的引用:
User keith = new User();
User justin = keith;
如果您有其他字段,其中keith
和justin
需要不同,但是jim
字段的值所引用的对象中的任何更改都必须被这两个字段看到,那么这将非常有用
这两个选项都是有效的,具体取决于您的需求
然而,有几点:
- 这意味着justin.jim的原始值是无意义的;您可以考虑添加一个<代码>用户< /COD>构造函数,使用该字段的初始值<代码>编号>代码>引用,允许您编写:
User keith = new User();
User justin = new User();
justin.jim = keith.jim;
- 像这样的共享可变状态可能变得难以推理,特别是在面对多个线程时
- 如注释中所述,您应该将
Number
类重命名为java.lang
(或者java.util
等,如果可能的话)
- 对于方法,您应该遵循Java命名约定,使用camelCase而不是PascalCase——我个人将它们称为
getValue
和setValue
现在两者都将引用同一个对象,在keith中所做的任何更改都将