java----类的继承 以学生类为题

定义一个学生类,有学生类派生出小学生初中生高中生大学生研究生四个类,其中大学生类再派生出一个本科生专科生两个子类,研究生再派生出硕士生和博士生两个子类

所有类:

//学生类
class student {
	int id;
	int classes;
	char sex;
	int year;

	public student(int id1, int classes1, char sex1, int year1) {
		this.id = id1;
		this.classes = classes1;
		this.sex = sex1;
		this.year = year1;
	}
	public void print(){
		System.out.println("学生类:"+id+" "+classes+" "+sex+" "+year+" ");
	}
}
// 小学生
class Primary extends student {
public Primary(int id1, int classes1, char sex1, int year1) {
	// TODO Auto-generated constructor stub
	super(id1, classes1, sex1, year1);	
}
public void print_Primary(){
	System.out.println("小学生类:"+id+" "+classes+" "+sex+" "+year+" ");
}
}

// 初中生
class Junior extends student {
	public Junior(int id1, int classes1, char sex1, int year1) {
		// TODO Auto-generated constructor stub
		super(id1, classes1, sex1, year1);	
	}
	public void print_Junior(){
		System.out.println("初中生类:"+id+" "+classes+" "+sex+" "+year+" ");
	}
}

// 高中生
class High extends student {
	public High(int id1, int classes1, char sex1, int year1) {
		// TODO Auto-generated constructor stub
		super(id1, classes1, sex1, year1);	
	}
	public void print_High(){
		System.out.println("高中生类:"+id+" "+classes+" "+sex+" "+year+" ");
	}
}

// 大学生
class College extends student {
	String vb,java;
	public College(int id1, int classes1, char sex1, int year1,String v,String j) {
		// TODO Auto-generated constructor stub
		super(id1, classes1, sex1, year1);	
		vb=v;
		java=j;
	}
	public void print_College(){
		System.out.println("大学生类:"+id+" "+classes+" "+sex+" "+year+" "+vb+" "+java+" ");
	}
}

// 专科
class College_Junior extends College {
public College_Junior(int id1, int classes1, char sex1, int year1,String v,String j){
	super(id1, classes1, sex1, year1,v,j);	
}
public void print_Junior(){
	System.out.println("专类:"+id+" "+classes+" "+sex+" "+year+" "+vb+" ");
}
}

// 本科
class College_High extends College {
	public College_High(int id1, int classes1, char sex1, int year1,String v,String j){
		super(id1, classes1, sex1, year1,v,j);	
	}
	public void print_High(){
		System.out.println("本类:"+id+" "+classes+" "+sex+" "+year+" "+java+" ");
	}
}

// 研究生
class Graduate extends student {
	public Graduate(int id1, int classes1, char sex1, int year1) {
		// TODO Auto-generated constructor stub
		super(id1, classes1, sex1, year1);	
	}
	public void print_College(){
		System.out.println("研究生类:"+id+" "+classes+" "+sex+" "+year+" ");
	}
}

// 硕士生
class Graduate_Master extends Graduate {
	public Graduate_Master(int id1, int classes1, char sex1, int year1) {
		// TODO Auto-generated constructor stub
		super(id1, classes1, sex1, year1);	
	}
	public void print_Master(){
		System.out.println("硕士生类:"+id+" "+classes+" "+sex+" "+year+" ");
	}
}

// 博士生
class Graduate_Doctoral extends Graduate {
	public Graduate_Doctoral(int id1, int classes1, char sex1, int year1) {
		// TODO Auto-generated constructor stub
		super(id1, classes1, sex1, year1);	
	}
	public void print_Doctoral(){
		System.out.println("博士生类:"+id+" "+classes+" "+sex+" "+year+" ");
	}
}


 

测试函数 main():

public class mMain {
	public static void main(String[] args){
		System.out.println("xx类: 学号     班级     性别     年龄");
		student s1=new student(23, 1111, '男', 21);
		s1.print();
		Primary p1=new Primary(23, 1111, '男', 21);
		p1.print_Primary();
		Junior j1=new Junior(23, 1111, '男', 21);
		j1.print_Junior();
		High h1=new High(23, 1111, '男', 21);
		h1.print_High();
		College c1=new College(23, 1111, '男', 21,"vb.net","java");
		c1.print_College();
		College_Junior cj1=new College_Junior(23, 1111, '男', 21,"vb.net","java");
		cj1.print_Junior();
		College_High ch1=new College_High(23, 1111, '男', 21,"vb.net","java");
		ch1.print_High();
		Graduate g1=new Graduate(23, 1111, '男', 21);
		g1.print_College();
		Graduate_Master gm1=new Graduate_Master(23, 1111, '男', 21);
		gm1.print_Master();
		Graduate_Doctoral gd1=new Graduate_Doctoral(23, 1111, '男', 21);
		gd1.print_Doctoral();
	}
}


 

运行效果:

java----类的继承 以学生类为题_第1张图片

 

你可能感兴趣的:(java----类的继承 以学生类为题)