java吧 关注:1,257,656贴子:12,752,194
  • 6回复贴,共1

请问子类中的构造方法中 this(name,"beijing",school);是啥意思

只看楼主收藏回复

private String location;
Person(String name){
this.name=name;
location="beijing";
}
Person(String name,String location){
this.name=name;
this.location=location;
}
public String info(){
return "name:"+name+ "location:"+location;
}
}
class Student extends Person{
private String school;
Student (String name,String school){
this(name,"beijing",school);
}
Student (String n,String l,String school){
super (n,l);
this.school=school;
} public String info(){
return super.info()+"school:"+school;
}
}
public class Test{
public static void main(String[] args){
Person p1= new Person("A");
Person p2= new Person("B","shanghai");
Student s1= new Student("C","s1");
Student s2 =new Student("C","shanghai","s1");
System.out.println(p1.info());
System.out.println(p2.info());
System.out.println(s1.info());
System.out.println(s2.info());
}
}


1楼2013-04-08 11:45回复
    调用自身的一个构造函数


    IP属地:广西4楼2013-04-08 12:12
    回复
      广告
      立即查看
      想想super就明白了


      IP属地:广东5楼2013-04-08 13:10
      收起回复
        this(1,2,3);可以理解为用本子类另外的构造方法构造Student s1
        super(1,2,3);可以理解为用基类的相对口参数的构造方法构造实例


        IP属地:山东6楼2013-07-08 22:39
        回复
          调用
          Student (String n,String l,String school){
          super (n,l);
          this.school=school;
          }


          IP属地:辽宁7楼2013-07-08 23:06
          回复