Java學習筆記7---父類構造方法有無參數對子類的影響


子類不繼承父類的構造方法,但父類的構造方法對子類構造方法的創建有影響。

具體來說就是:

①.當父類沒有無參構造方法時:

    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顯式調用。

 

待學習:訪問權限修飾符的問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM