public class Test { private String name; public Test(String name) { // name=name;//这个时候就近访问了,方法里面有,外面就不会访问了!这个时候打印为空 this.name=name; } public void speak() { System.out.println(name); }
//测试类 public class examp01 { public static void main(String[] args) { // TODO Auto-generated method stub Test test = new Test("bowen"); test.speak(); } }