super函数
Super()表示调用父类的构造方法。若是没有定义构造方法,那么就会调用父类的无参构造方法,即super()。this
thisspa
在构造方法中,this表示本类的其余构造方法:
student(string n){code
this(); //this表示调用这个类的student()
}
若是调用student(int a)则为this(int a)。
特别注意:用this调用其余构造方法时,this必须为第一条语句,而后才是其余语句。blog
1 class Person{ 2 3 Person(){ 4 prt("A Person."); 5 } 6 7 Person(String name){ 8 prt("A person name is:"+name); 9 } 10 } 11 12 public class Chinese extends Person{ 13 14 class Chinese{ 15 16 Chinese(){ 17 super(); //调用父类构造函数 18 prt("A chinese."); 19 } 20 21 Chinese(String name){ 22 super(name); // 调用父类具备相同形参的构造函数,Person(String name)里的prt("A person name is:"+name)方法被调用。 23 prt("his name is:"+name); 24 } 25 26 Chinese(String name,int age){ 27 this(name);//调用当前具备相同形参的构造函数,Chinese(String name)里prt("his name is:"+name);super(name);被调用。 28 prt("his age is:"+age); 29 } 30 31 32 }