2
public class FooClass {
BarClass bar = null;
int a = 0;
int b = 1;
int c = 2;
public FooClass(BarClass bar) {
this.bar = bar;
bar.setFoo(this);
}
}
public class BarClass {
FooClass foo = null;
public BarClass(){}
public void setFoo(FooClass foo) {
this.foo = foo;
}
}
別處......如何在多個類之間傳遞對象? Java的
BarClass theBar = new BarClass();
FooClass theFoo = new FooClass(theBar);
theFoo.a //should be 0
theBar.foo.a = 234; //I change the variable through theBar. Imagine all the variables are private and there are getters/setters.
theFoo.a //should be 234 <-----
我怎麼能傳遞一個對象到另一個類,做出改變,並且有變化出現在第一個對象的原始實例?
或
我怎樣才能使其中一個變化的一類是體現在其他類中的循環?
你試過了嗎?這正是你想要的方式。 – Howard