设计一个学生类Student

时间:2020-12-18 21:58:01
package 第四章;
/*
File name:TestStudent.cpp
Author:杨柳
Date:2017/10/11
IDE:eclipse
description:学生类
github:
*/
public class TestStudent {
public static void main(String agsp[]){
Student stu=new Student("杨柳",18,"高中");
stu.show();
System.out.print("-------------------------");
System.out.println();
Undergraduate under=new Undergraduate("王梦凡",22,"本科","软件工程");
under.show();
System.out.print("-------------------------");
System.out.println();
Graduate gra=new Graduate("王志云",26,"研究生","人工智能");
gra.show();
}

}
class Student{ //学生类Student
String name; //姓名
int age; //年龄
String education; //学历
Student(String na, int ag, String edu) { //构造方法
this.name = na;
this.age = ag;
this.education = edu;
}
public void show() {
System.out.println("姓名:"+name+" "+"年龄:"+age+" "+"学历:"+education);
}
}

class Undergraduate extends Student{ //本科生类
String specialty;
Undergraduate(String na, int ag, String edu,String spe) {
super(na, ag, edu);
this.specialty = spe;
// TODO 自动生成的构造函数存根
}
public void show() {
super.show();
System.out.println("专业:"+specialty);
}
}
class Graduate extends Student{ //研究生
String direction;
Graduate(String na, int ag, String edu,String dir) {
super(na, ag, edu);
this.direction=dir;
// TODO 自动生成的构造函数存根
}
public void show() {
super.show();
System.out.println("研究方向:"+direction);
}

}

2.设计一个学生类Student,其属性有:姓名(name)、 年龄 (age)、 学历 (education), 由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类增加属性:专业 (specialty), 研究生类增加属性:研究方向 (direction)。 每个类都有构造方法和用于输出属性信息的show () 方法,在测试类TestExtends中测试输出

相关文章