码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习笔记七---父类构造方法有无参数对子类的影响

时间:2017-10-17 09:55:46      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:undefined   []   调用   struct   学习   xtend   lap   test   创建   

子类不继承父类的构造方法,但父类的构造方法对子类构造方法的创建有影响。具体来说就是:

①.当父类没有无参构造方法时,子类也不能有无参构造方法;且必须在子类构造方法中显式以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学习笔记七---父类构造方法有无参数对子类的影响

标签:undefined   []   调用   struct   学习   xtend   lap   test   创建   

原文地址:http://www.cnblogs.com/chanchan/p/7679716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!