1. 程式人生 > >Java學習筆記七---父類構造方法有無參數對子類的影響

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

als core npr 筆記 java學習筆記 def 權限 必須 void

子類不繼承父類的構造方法,但父類的構造方法對子類構造方法的創建有影響。具體來說就是:

①.當父類沒有無參構造方法時,子類也不能有無參構造方法;且必須在子類構造方法中顯式以super(參數)的形式調用父類構造方法。否則會出現如下的錯誤:

Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor

子類在有參構造方法中顯式調用super(參數)後,如果再寫一個無參構造方法,則會出現下面的錯誤:

Implicit super constructor Person() is undefined. Must explicitly invoke another constructor

②.父類有無參構造方法時,子類可以有無參構造方法,也可以有有參構造方法;在有參構造方法中,可以用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 " +this.getName());
		System.out.println("I am " + this.getAge() +" years old");
		
		if(this.getGender() == "female")
			System.out.println("I am a girl");
		else
			if(this.getGender() == "male")
				System.out.println("I am a boy");
			else
				System.out.println("Something is wrong!");
		System.out.println("My hobby is " + this.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 " + this.stuNumber);
		System.out.println("My score is " + this.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();
	}

}

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

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