classConstructSequence
{publicstaticvoid main(String[] args){
Person p = new Student("LiMing", 18, "PKU");
}
}
classPerson
{
String name="unnamed"; //step 2int age=-1;
Person( String name, int age ){
super(); //step 1//step 3
System.out.println( "start to create the constructor Person(),and now this.name="+this.name+",this.age="+this.age );
this.name=name; this.age=age;
System.out.println( "Person() constructed completely,and now this.name="+this.name+",this.age="+this.age );
}
}
classStudentextendsPerson
{
String school="unnamed"; //step2
Student( String name, int age, String school ){
super( name, age ); //step 1//step 3
System.out.println( "start to create the constructor Student(),and now this.name="+this.name+",this.age="+this.age+",this.school="+this.school );
this.school = school;
System.out.println( "Student() constructed completely,and now this.name="+this.name+",this.age="+this.age+",this.school="+this.school );
}
}
-----------OUTPUT-----------
start to create the constructor Person(),and now this.name=unnamed,this.age=-1
Person() constructed completely,and now this.name=LiMing,this.age=18
start to create the constructor Student(),and now this.name=LiMing,this.age=18,this.school=unnamed
Student() constructed completely,and now this.name=LiMing,this.age=18,this.school=PKU