定义Student学生类,拥有学号、姓名、性别属性,提供构造函数,以及相应属性的get set函数,提供函数attendClass(String className)表示上课。
定义CollegeStudent大学生类继承自Student类,拥有新增属性专业,提供构造函数,提供新增属性的get和set函数
定义GraduateStudent研究生类继承自CollegeStudent类,拥有新增属性导师,提供构造函数,提供新增属性的get和set函数,提供函数doResearch() 表示做研究(打印xx is doing research)。
main函数中对构造的类进行测试
输入描述:
学生类信息,学号、姓名、性别
大学生类信息,学号、姓名、性别、专业
研究生类信息,学号、姓名、性别、专业、导师
输出描述:
学生类信息
大学生类信息
研究生类信息
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
String name = scan.next();
String sex = scan.next();
Student s = new Student(no, name, sex);
s.print();
no = scan.nextInt();
name = scan.next();
sex = scan.next();
String major = scan.next();
CollegeStudent c = new CollegeStudent(no, name, sex, major);
c.print();
no = scan.nextInt();
name = scan.next();
sex = scan.next();
major = scan.next();
String supervisor = scan.next();
GraduateStudent g = new GraduateStudent(no, name, sex, major, supervisor );
g.print();
g.doResearch();
scan.close();
}
}
/* 你的代码被嵌在这里*/
输入样例:
在这里给出一组输入。例如:
1 liu female
2 chen female cs
3 li male sc wang
输出样例:
在这里给出相应的输出。例如:
no: 1
name: liu
sex: female
no: 2
name: chen
sex: female
major: cs
no: 3
name: li
sex: male
major: sc
supervisor: wang
li is doing research
code:
class Student{
int no;
String name;
String sex;
public Student(int no,String name,String sex) {
this.no=no;
this.name=name;
this.sex=sex;
}
public void print() {
System.out.println("no: "+this.no);
System.out.println("name: "+this.name);
System.out.println("sex: "+this.sex);
}
}
class CollegeStudent extends Student{
String major;//专业
public CollegeStudent(int no, String name, String sex,String major) {
super(no, name, sex);//调用super
this.major=major;
}
public void print() {
super.print();//调用super
System.out.println("major: "+this.major);
}
}
class GraduateStudent extends CollegeStudent{
String t;//导师
public GraduateStudent(int no, String name, String sex, String major,String t) {
super(no, name, sex, major);
this.t=t;
}
public void print() {
super.print();
System.out.println("supervisor: "+this.t);
}
public void doResearch() {
System.out.println(this.name+" is doing research");
}
}