public class test {
public static void main(String[] args) {
A a = new A();
B b = new B();
System.out.println(a);
System.out.println(b);
change(a,b); //实现 a b交换
System.out.println(a);
System.out.println(b);
}
}
class A{
}
class B{
}
交换的前提应该两个对象的类型是相同的吧~~
那么应该是:
A a = new A();
A b = new A();
1.有人说这种方法可以直接交换
change(a,b){
A c= a;
a = b;
b = c;
}
2.有人说这种方法不行 那么就有以下方法:
change(a,b){
A c =new A();
c.setXX(a.getXX);
a.setXX(b.getXX);
b.setXX(c.getXX);
}
试下吧