
这个 this 到底指向谁, 为什么 s.getName() 输出 foo, 而 s.name 输出 bar.
我以为两个都结果都应该是 bar,是哪里搞错了?
class Person { public String name = "foo"; public String getName() { return this.name; } } class Student extends Person { public String name = "bar"; } public class Hello { public static void main(String[] args) { Student s = new Student(); System.out.println(s.getName()); // foo System.out.println(s.name); // bar } } 1 yumenawei Oct 12, 2020 帮顶~ |
2 billlee Oct 12, 2020 |
3 itechify PRO 多态针对接口(行为,方法),属性没有多态 |
4 dushe Oct 13, 2020 this 指向最近的对象 |