例4-7和例4-8
class Tom{
void f(int x,double y){
x=x+1;
y=y+1;
System.out.printf("参数x和y的值分别是:%d,%3.2f\n",x,y);
}
}
public class Example4_7{
public static void main(String args[]){
int x=10;
double y=12.58;
Tom cat=new Tom();
cat.f(x,y);
System.out.printf("main方法中x和y的值仍然分别是:%d,%3.2f\n",x,y);
}
}
class Jerry{
int leg;
Jerry(int n){
leg=n;
}
void setLeg(int n){
leg=n;
}
int getLeg(){
return leg;
}
}
class Tom{
void f(Jerry mouse){
mouse.setLeg(12);
System.out.println("在执行方法f时,参数mouse修改了自己leg的值");
System.out.println("当前参数mouse的成员leg的值:"+mouse.getLeg());
mouse=null;
//mouse.setLeg(12);
}
}
public class Example4_8{
public static void main(String args[]){
Tom cat=new Tom();
Jerry jerry=new Jerry(2);
System.out.println("在调用方法f之前,jerry的成员leg的值:"+jerry.getLeg());
cat.f(jerry);
System.out.println("在调用方法f之后,jerry的成员leg的值:"+jerry.getLeg());
}
}