日期:2014-05-20  浏览次数:21181 次

java新手急求大神解答!!
class PersonDemo {
  private static String name;
private static int age; 
static {
System.out.println(name+" "+age);  
}
PersonDemo(String name) {
this.name = "name";
System.out.println(name);
}
PersonDemo(int age) {
this.age = age;
System.out.println(age);
}
public void sop() {
System.out.println(name+" "+age);
  }
}
public class Person {
public static void main(String[] args) {
PersonDemo p = new PersonDemo("zhangsan");
PersonDemo p1 = new PersonDemo(4);
p.sop();
}
}
最后p.sop();输出的时候为什么name的值没有赋上,age都赋上了啊

------解决方案--------------------
因为构造方法里边写的是:this.name = "name";
赋的是字符串 “name”;而不是楼主传的参数"zhangsan"