java函数体参数传递

一个简单的例子:
实体类A,含一个成员变量num

public class A {
    private int num;
    
public A(int num){
    this.num=num;
}

public void setNum(int num){
    this.num=num;
}
public int getNum(){
    return num;
}

}

测试类含有两个函数,用于操作实体A

public class Main {
    public static void fun1(A a){
        a.setNum(100);
    }
    
    public static void fun2(A a){
        A b=new A(101);
        a=b;
    }
}

实例化A后,将A作为参数传递到fun中,查看执行结果

    public static void main(String[] args) {
        A a=new A(99);
        System.out.println(a.getNum());
        fun1(a);
        System.out.println(a.getNum());
        fun1(a);
        System.out.println(a.getNum());
    }

结果为:

99
100
100

你可能感兴趣的:(java函数体参数传递)