永久java对象
我想知道(必须有一个简单的解决方案)是否有一种方法可以创建多个对象,并在其他类和这些类中的对象中使用它们,而无需更新它们(在 Java 中)。一个例子:
类 A、B 和 C 生成对象 ab 和 c。 然后我有类 D,它生成对象 d。 现在我想在a中使用d,更改它,在b中使用d,在那里更改它并在c中使用d,并且仍然保留a和b中的所有更改。 我知道可以在函数中使用 d 作为参数并返回 d,但这在我目前正在制作的程序中没有用。
有什么帮助吗?
好的,我的代码示例:
public class A {
D d = new D ();
A(D dObject){
this.d = dObject;
}
public void add () {
DObject.add(5);
}
}
public class B {
D d = new D ();
B(D dObject){
this.d = dObject;
}
public void add () {
DObject.add(5);
}
}
public class C {
D d = new D ();
C(D dObject){
this.d = dObject;
}
public void add () {
DObject.add(5);
}
}
public class D {
public int x=0;
public void add (int y){
x +=y;
}
}
// in main class:
D d = new D();
A a = new A(d);
B b = new B(d);
C c = new C(d);
a.add();
b.add();
c.add();
// d.x should give 15 now.
I was wondering (there has to be an easy solution) if there is a way to make a number of objects, and use them in other classes and objects from those classes, without having to update them (in Java). An example:
Class A, B and C make objects a b and c.
Then I have class D, which makes object d.
Now I want to use d in a, change it, use d in b, change it there and use d in c and still have all the changes from a and b.
I know it's possible with using d as an argument in the functions, and returning d, but that is not useful in the program I'm making at the moment.
Any help?
Ok, My example in code:
public class A {
D d = new D ();
A(D dObject){
this.d = dObject;
}
public void add () {
DObject.add(5);
}
}
public class B {
D d = new D ();
B(D dObject){
this.d = dObject;
}
public void add () {
DObject.add(5);
}
}
public class C {
D d = new D ();
C(D dObject){
this.d = dObject;
}
public void add () {
DObject.add(5);
}
}
public class D {
public int x=0;
public void add (int y){
x +=y;
}
}
// in main class:
D d = new D();
A a = new A(d);
B b = new B(d);
C c = new C(d);
a.add();
b.add();
c.add();
// d.x should give 15 now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看单例模式
它仅在您想要 D 类的单个实例时才有效,但您可以创建一个单例来存储其他对象的引用。希望这有帮助。
Take a look at the Singleton pattern
It only works if you want a single instance of class D, but you can create a singleton to store references of other objects. Hope this helps.
我不确定我是否理解正确 - 如果您更正语法错误,您发布的代码将完全按照您所说的进行(
dx
是 15)。这是一个应该可行的版本:
如果您希望 d 保持不变而不是更改(例如最后的 dx == 0 ),您可以执行以下操作之一:
dObject
,因此每个都有自己的D
对象。D
不可变,并让其add
方法返回一个新的D
对象。A
、B
、C
的add
方法将实现d = d.add (5);
。I'm not sure if I understand right - the code you posted does exactly what you say (
d.x
is 15), if you correct the syntax errors.Here is a version that should work:
If you want
d
to stay the same instead of changing (e.g.d.x == 0
at the end), you can do one of these:dObject
in the constructor of A, B, C, so each has its ownD
object.D
immutable and let itsadd
method instead return a newD
object. Theadd
method ofA
,B
,C
would then have the implementationd = d.add(5);
.根据您的描述,我认为您的意思是不可变对象。
I think you mean Immutable Objects based on your description.