class Test1 { public static void change(Test2 test) { test.setNo(4); } public static void main(String[] args) { Test2 test = new Test2(3); Test1.change(test); System.out.println(test.getNo()); } }class Test2 { public Test2(int num) { this.no = num; } public void setNo(int num) { this.no = num; } public int getNo() { return this.no; } private int no; }这个能说明问题吗?
我认为这是一个java参数传递的问题,java中对于基本类型的参数和对象类型的参数采用了不同的处理方式。对于基本类型是:传值,即将参数的值复制一份,方法里用复件,方法外用原件,在方法里面可以改变复件,但原件不受影响;原件改变了,也不影响复件。对于对象类型是:引用传递,即将“参数的引用”复制一份,这时两个引用指向的是同一个对象,所以改变任一个的值都会使两个引用的值受到影响。 不知道下面这个程序是否能说明你的问题public class PassParamter { public PassParamter() { }
public void methodA(ComplexNumber comNum) { //这个引用指向了一个新的对象 comNum = new ComplexNumber(1, 2); }
public void methodB(ComplexNumber comNum) { //这个引用还是同一个对象,修改后原对象也改变了 comNum.setRealPart(1); comNum.setRealPart(2); }
public void methodC(int num) { //对于基本数据对象,传入的是值的复件,因此,原对象不变 num++; }
public static void main(String[] args) { PassParamter test = new PassParamter(); ComplexNumber comNum = new ComplexNumber(5, 5); System.out.println("调用methodA方法之前,comNum : " + comNum.toString()); test.methodA(comNum); System.out.println("调用methodA方法之后,comNum : " + comNum.toString()); System.out.println("调用methodB方法之前,comNum : " + comNum.toString()); test.methodB(comNum); System.out.println("调用methodB方法之后,comNum : " + comNum.toString()); int num = 0; System.out.println("调用methodC方法之前,num : " + num); test.methodC(num); System.out.println("调用methodC方法之后,num : " + num); }}
比如传过去2个javabean。
void testmethod(Bean1 b1,Bean2 b2){
b1.setxxx();
b2.setxxx();//外面调用的b1,b2会发生改变。
//b2=new Bean2(); b2.setyyyy(); 这样不行,外面的不会发生变化
}
比如传过去2个javabean。
void testmethod(Bean1 b1,Bean2 b2){
b1.setxxx();
b2.setxxx();//外面调用的b1,b2会发生改变。
//b2=new Bean2(); b2.setyyyy(); 这样不行,外面的不会发生变化
}
顶
class Test1
{
public static void change(Test2 test)
{
test.setNo(4);
}
public static void main(String[] args)
{
Test2 test = new Test2(3);
Test1.change(test);
System.out.println(test.getNo());
}
}class Test2
{
public Test2(int num)
{
this.no = num;
}
public void setNo(int num)
{
this.no = num;
}
public int getNo()
{
return this.no;
}
private int no;
}这个能说明问题吗?
不知道下面这个程序是否能说明你的问题public class PassParamter {
public PassParamter() {
}
public void methodA(ComplexNumber comNum) {
//这个引用指向了一个新的对象
comNum = new ComplexNumber(1, 2);
}
public void methodB(ComplexNumber comNum) {
//这个引用还是同一个对象,修改后原对象也改变了
comNum.setRealPart(1);
comNum.setRealPart(2);
}
public void methodC(int num) {
//对于基本数据对象,传入的是值的复件,因此,原对象不变
num++;
}
public static void main(String[] args) {
PassParamter test = new PassParamter();
ComplexNumber comNum = new ComplexNumber(5, 5);
System.out.println("调用methodA方法之前,comNum : " + comNum.toString());
test.methodA(comNum);
System.out.println("调用methodA方法之后,comNum : " + comNum.toString());
System.out.println("调用methodB方法之前,comNum : " + comNum.toString());
test.methodB(comNum);
System.out.println("调用methodB方法之后,comNum : " + comNum.toString());
int num = 0;
System.out.println("调用methodC方法之前,num : " + num);
test.methodC(num);
System.out.println("调用methodC方法之后,num : " + num);
}}