当前位置: 技术问答>java相关
关于JAVA类方法中参数据传递
来源: 互联网 发布时间:2015-03-01
本文导语: 请问各位同仁,能否给Java类方法中的参数传址 如何实现. 希望大家指点! | 以对象为参数,传递对象的引用首地址 class ValHold{ public int i = 10; } public class ObParm{ public static void ...
请问各位同仁,能否给Java类方法中的参数传址
如何实现.
希望大家指点!
如何实现.
希望大家指点!
|
以对象为参数,传递对象的引用首地址
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
ValHold v = new ValHold();
v.i=10;
System.out.println("Before another = "+ v.i);
another(v);
System.out.println("After another = "+ v.i);
}//End of amethod
public void another(ValHold v){
v.i = 20;
System.out.println("In another = "+ v.i);
}//End of another
}
输出结果:
Before another = 10
In another = 20
After another = 20
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
ValHold v = new ValHold();
v.i=10;
System.out.println("Before another = "+ v.i);
another(v);
System.out.println("After another = "+ v.i);
}//End of amethod
public void another(ValHold v){
v.i = 20;
System.out.println("In another = "+ v.i);
}//End of another
}
输出结果:
Before another = 10
In another = 20
After another = 20
|
以变量为参数,传递值
public class Parm{
public static void main(String argv[]){
Parm p = new Parm();
p.amethod();
}//End of main
public void amethod(){
int i=10;
System.out.println("Before another i= " +i);
another(i);
System.out.println("After another i= " + i);
}//End of amethod
public void another(int i){
i+=10;
System.out.println("In another i= " + i);
}//End of another
}
输出结果:
Before another i= 10
In another i= 20
After another i= 10
public class Parm{
public static void main(String argv[]){
Parm p = new Parm();
p.amethod();
}//End of main
public void amethod(){
int i=10;
System.out.println("Before another i= " +i);
another(i);
System.out.println("After another i= " + i);
}//End of amethod
public void another(int i){
i+=10;
System.out.println("In another i= " + i);
}//End of another
}
输出结果:
Before another i= 10
In another i= 20
After another i= 10