本帖最后由 戴乾刚 于 2013-3-13 20:17 编辑
- package heima.com;
- public class ChangeDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Person p1 = new Person("张三",32);
- Person p2 = new Person("李四",33);
- System.out.println(p1.getName()+p1.getAge());
- System.out.println(p2.getName()+p2.getAge());
- System.out.println(p1);
-
- /*Person temp = p1;
- p1 = p2;
- p2 = temp;*/
- swap(p1,p2); //这两种交换方式有什么区别?
-
- System.out.println(p1.getName()+p1.getAge());
- System.out.println(p2.getName()+p2.getAge());
- System.out.println(p1);
- }
- public static void swap(Person p1,Person p2){
- Person temp = p1;
- p1 = p2;
- p2 = temp;
- }
- }
- class Person{
- private String name;
- private int age;
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String getName(){
- return name;
- }
- public int getAge(){
- return age;
- }
- }直接在主函数中交换和调用swap()方法为什么结果不同
复制代码 |