public class main {
public static void main(String[] args) {
// TODO Auto-generated constructor stub
B b = new B();
ArrayList<Integer> mb = b.get();
b.add();
A a = new A();
ArrayList<Integer> ma = a.get();
ma = mb;
a.add();
System.out.println(a.get());
System.out.println(b.get());
}
}
import java.util.ArrayList;
public class A {
ArrayList<Integer> m = new ArrayList<Integer>();
public A() {
}
public void add() {
m.add(10);
}
public ArrayList<Integer> get() {
return m;
}
}
import java.util.ArrayList;
public class B {
ArrayList<Integer> m = new ArrayList<Integer>();
public void add() {
m.add(12);
}
public ArrayList<Integer> get() {
return m;
}
}
结果:
[10]
[12]
import java.util.ArrayList;
public class main {
public static void main(String[] args) {
// TODO Auto-generated constructor stub
B b = new B();
ArrayList<Integer> m = b.get();
A a = new A(m);
a.add();
b.add();
System.out.println(a.get());
System.out.println(b.get());
}
}
import java.util.ArrayList;
public class A {
ArrayList<Integer> m = new ArrayList<Integer>();
public A(ArrayList<Integer> m) {
this.m = m;
}
public void add() {
m.add(10);
}
public ArrayList<Integer> get() {
return m;
}
}
import java.util.ArrayList;
public class B {
ArrayList<Integer> m = new ArrayList<Integer>();
public void add() {
m.add(12);
}
public ArrayList<Integer> get() {
return m;
}
}
结果:
[10, 12]
[10, 12]