子类不继承父类的构造方法,但父类的构造方法对子类构造方法的创建有影响。
具体来说就是:
①.当父类没有无参构造方法时:
1).子类也不能有无参构造方法;
2).且必须在子类构造方法中显式以super(参数)的形式调用父类构造方法;
否则会出现如下的错误:
Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor
3).子类在有参构造方法中显式调用super(参数)后,如果再写一个无参构造方法,则会出现下面的错误:
Implicit super constructor Person() is undefined. Must explicitly invoke another constructor
②.父类有无参构造方法时:
1).子类可以有无参构造方法;
2).也可以有有参构造方法;在有参构造方法中,可以用super显式调用父类构造方法也可以不调用;
也就是说,这时候,子类在构造方法的创建上是比较*的。
下面是简单示例:
有两个类,Person类和Student类,Student类继承自Person类。两个类的构造方法详见代码。
Person类:
package human; public class Person {
String name;
int age;
String gender; private String hobby; public Person() { } public Person(String n, String g) {
this.name = n;
this.gender = g;
} public Person(String n, int a, String g, String h) {
this.name = n;
this.age = a;
this.gender = g;
this.hobby = h;
} public void setName(String n) {
this.name = n;
} public void setAge(int a) {
this.age = a;
} public void setGender(String g) {
this.gender = g;
} public void setHobby(String h) {
this.hobby = h;
} public String getName() {
return this.name;
} public int getAge() {
return this.age;
} public String getGender() {
return this.gender;
} public String getHobby() {
return this.hobby;
} public void informationPrint() {
System.out.println("My name is " +getName());
System.out.println("I am " + getAge() +" years old"); if(getGender() == "female")
System.out.println("I am a girl");
else
if(getGender() == "male")
System.out.println("I am a boy");
else
System.out.println("Something is wrong!");
System.out.println("My hobby is " + hobby);
} }
Student类:
package human; public class Student extends Person {
String stuNumber;
int score; public Student() { } public Student(String n, String g) {
super(n,g);
} public Student(String n, int a, String g, String h) {
super(n,a,g,h);
} public Student(String sN, int s) {
this.stuNumber = sN;
this.score = s;
} public Student(String n, String g, String sN, int s) {
super(n,g);
this.stuNumber = sN;
this.score = s;
} public Student(String n, int a, String g, String h, String sN, int s) {
super(n,a,g,h);
this.stuNumber = sN;
this.score = s;
} public void setStuNumber(String num) {
this.stuNumber = num;
} public void setScore(int s) {
this.score = s;
} public String getStuNumber() {
return this.stuNumber;
} public int getScore() {
return this.score;
} public void informationPrint() {
super.informationPrint();
System.out.println("My number is " + stuNumber);
System.out.println("My score is " + score);
} }
测试类:
package human; public class TestMain { public static void main(String[] args) {
Person xiaoxiP = new Person("xiaoxiP",29,"female","piano");
Person xiaonanP = new Person("xiaonanP","male");
Student xiaoxiS = new Student("xiaoxiS",28,"female","piano","124",90);
Student xiaonanS = new Student("xiaonanS","male","123",98); xiaoxiP.informationPrint();
xiaoxiS.informationPrint(); xiaonanP.informationPrint();
xiaonanS.informationPrint();
} }
结果:
My name is xiaoxiP
I am 29 years old
I am a girl
My hobby is piano
My name is xiaoxiS
I am 28 years old
I am a girl
My hobby is piano
My number is 124
My score is 90
My name is xiaonanP
I am 0 years old
I am a boy
My hobby is null
My name is xiaonanS
I am 0 years old
I am a boy
My hobby is null
My number is 123
My score is 98
总结(追加):
①.父类没有无参构造方法时,子类也不能有无参构造方法,必须用super显式调用父类构造方法。
②.父类没有无参构造方法且有多个有参构造方法时,子类可以只显式调用父类的一个构造方法;
子类也可以构造多个构造方法,只要保证每个构造方法都显式调用了父类构造方法就可以,对于调用父类哪一个构造方法则没有要求。
③.父类有无参构造方法时,子类可以有无参构造方法,也可以有有参构造方法;
既可以用super显式调用父类构造方法,也可以不用super显式调用。
待学习:访问权限修饰符的问题。