Discuss / Java / 参见前面关于变量的章节

参见前面关于变量的章节

Topic source

参见前面的变量指向章节,如果把代码改成如下,则还会指向bob。

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        String bob = "Bob";
        p.setName(bob); // 传入bob变量
        System.out.println(p.getName()); // "Bob"
        String alice = "Alice";
        bob = alice; // bob改名为Alice
        System.out.println(p.getName()); // "Bob"还是"Alice"?
    }
}

class Person {
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


  • 1

Reply