很多书籍都说Java支持传引用调用的方式,类似于C++中的Person &a引用调用,而近来编程遇到一系列问题让我对此产生了怀疑,于是将这些方法一一列出,我们来一起看看JAVA中的调用方式:
看下面的程序:
class Person {
private String name;//姓名
private String sex;//性别
public Person(String x, String y) {
this.name = x;
this.sex = y;
}
public void setStatus(String x, String y) {
this.name = x;
this.sex = y;
}
public String toString() {
return name + sex;
}
// -----交换普通对象-----
public static void changeref(Person tmpx, Person tmpy) {
//交换tmpx和tmpy对象
Person swapref = tmpx;
tmpx = tmpy;
tmpy = swapref;
// System.out.println("在方法中交换的结果: refa =" + tmpx.toString());
// System.out.println("在方法中交换的结果: refb =" + tmpy.toString());
}
// ----- 交换数组对象-----
public static void changeArrayRef(Person[] x, Person[] y) {
//交换数组对象
Person swaparrayref = x[x.length-1];
x[x.length-1] =y[x.length-1];
y[x.length-1] = swaparrayref;
}
//-----交换数组-----
public static void changeArray(int[] x,int[] y) {
int[] tmp =x;
x = y;
y = tmp;
&nbs
看下面的程序:
class Person {
private String name;//姓名
private String sex;//性别
public Person(String x, String y) {
this.name = x;
this.sex = y;
}
public void setStatus(String x, String y) {
this.name = x;
this.sex = y;
}
public String toString() {
return name + sex;
}
// -----交换普通对象-----
public static void changeref(Person tmpx, Person tmpy) {
//交换tmpx和tmpy对象
Person swapref = tmpx;
tmpx = tmpy;
tmpy = swapref;
// System.out.println("在方法中交换的结果: refa =" + tmpx.toString());
// System.out.println("在方法中交换的结果: refb =" + tmpy.toString());
}
// ----- 交换数组对象-----
public static void changeArrayRef(Person[] x, Person[] y) {
//交换数组对象
Person swaparrayref = x[x.length-1];
x[x.length-1] =y[x.length-1];
y[x.length-1] = swaparrayref;
}
//-----交换数组-----
public static void changeArray(int[] x,int[] y) {
int[] tmp =x;
x = y;
y = tmp;
&nbs
| 对此文章发表了评论 |
