1. 程式人生 > >子類構造方法呼叫父類構造方法(super關鍵字的使用)

子類構造方法呼叫父類構造方法(super關鍵字的使用)

package kaoshi;

/**
 ************************************
 * @author Hejing
 * @date   2017年12月24日
 * @class  fisrt.java
 * 
 ************************************
 */
class Student{
	String name;
	int age;
	
	public Student(String name,int age) {
		this.name=name;
		this.age=age;
		
	}
	
	public void study() {
		System.out.println("學生的學習方法是:");
	}
	public void show() {
		System.out.println("姓名是"+name);
		System.out.println("年齡是"+age);	
	}
}
class Benkesheng extends Student{
	String dept;
	String disagree;
	public Benkesheng(String name,int age,String dept,String disagree) {
		super(name, age);
		this.dept=dept;
		this.disagree=disagree;
		
	}
	
	public void study() {
		System.out.println("本科生學習方法是:");
	}
	public void show() {
		super.show();
		System.out.println("專業是"+dept);
		System.out.println("學位是"+disagree);
	}
}
class Yanjiusheng extends Benkesheng{
	String direction;
	public  Yanjiusheng(String name,int age,String dept,String disagree,String direction) {
		super(name, age,dept,disagree);
		this.direction =direction;
		
	}
	
	public void study() {
		System.out.println("研究生學習方法是:");
	}
	public void show() {
		super.show();
		System.out.println("方向"+direction);
	}
}
public class fisrt {
public static void main(String[] args) {
	Student s1=new Student("張三",18);
	Benkesheng b1=new Benkesheng("張思",20,"計算機","軟體學位");
	Yanjiusheng y1=new  Yanjiusheng("張wu",20,"計算機","軟體學位","人工智慧");
	s1.study();
	s1.show();
	b1.study();
	b1.show();
	y1.study();
	y1.show();
	
}
}