首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

请问,为什么swap1不能达到交换的目的呢

2012-01-07 
请教,为什么swap1不能达到交换的目的呢Show the output of the following program:public class Test {pub

请教,为什么swap1不能达到交换的目的呢
Show the output of the following program: 
public class Test {
  public static void main(String[] args) {
  Circle circle1 = new Circle(1);
  Circle circle2 = new Circle(2);

  swap1(circle1, circle2);
  System.out.println("After swap1: circle1 = " +
  circle1.radius + " circle2 = " + circle2.radius);

  swap2(circle1, circle2);
  System.out.println("After swap2: circle1 = " +
  circle1.radius + " circle2 = " + circle2.radius);
  }

  public static void swap1(Circle x, Circle y) {
  Circle temp = x;
  x = y;
  y = temp;
  }

  public static void swap2(Circle x, Circle y) {
  double temp = x.radius;
  x.radius = y.radius;
  y.radius = temp;
  }
}

class Circle {
  double radius;

  Circle(double newRadius) {
  radius = newRadius;
  }
}

为什么swap1不能达到交换的目的呢


[解决办法]
swap1中x,y只是引用,交换的只是引用的值,而x,y指向的值并为发生变化!
[解决办法]
方法参数是 传值不是传引用
[解决办法]
方法参数传递的是对象引用的copy?
[解决办法]
java 中的函数调用参数是传值调用。
[解决办法]
传值改成传引用
[解决办法]
swap1中把x,y交换了,但没换circle1,circle2
[解决办法]
值传递 为什么会变啊

热点排行