今天在做educoder上的题目的时候无意发现了自己的一个知识漏洞
上代码
1 class Person{ 2 String name = "张三"; 3 int age = 18; 4 String sex = "男"; 5 6 public Person(String name,int age,String sex){ 7 this(age); 8 name = name; 9 System.out.println(name); 10 sex = sex; 11 } 12 }
13 。。。
14 Person person = new Person("Hua", 18, "女");
刚开始我以为9行会输出张三,其实错了,会输出Hua,所以要调用类中的字段的话要用this.name
原文:https://www.cnblogs.com/WildSky/p/11558852.html