java变量引用问题

class Test {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class test1 {

    public static void main(String[] args){
        Test test = new Test();
        test.setName("cc");
        System.out.println("1:"+test.getName());

        test1 t1 = new test1();
        System.out.println(t1.f(test));
        System.out.println("2:"+test.getName());
        System.out.println(t1.f2(test));
        System.out.println("3:"+test.getName());
    }
    public String f(Test test){
        test.setName("zz");
        return test.getName();
    }
    public String f2(Test test){
        test = new Test();
        test.setName("zz");
        return test.getName();
    }

}

早上看到一篇文章,回忆下,记录下,这里看似传得是值,实则是引用

答案:

1:cczz2:zzzz3:zz


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页