125 - 学生、大学生、
Description
定义Student学生类,拥有学号、姓名、性别属性,提供构造函数,以及相应属性的get set函数,提供函数attendClass(String className)表示上课。 定义CollegeStudent大学生类继承自Student类,拥有新增属性专业,提供构造函数,提供新增属性的get和set函数 定义GraduateStudent研究生类继承自CollegeStudent类,拥有新增属性导师,提供构造函数,提供新增属性的get和set函数,提供函数doResearch() 表示做研究(打印xx is doing research)。 main函数中对构造的类进行测试
Input
学生类信息,学号、姓名、性别 大学生类信息,学号、姓名、性别、专业 研究生类信息,学号、姓名、性别、专业、导师
Output
学生类信息 大学生类信息 研究生类信息
Sample Input
1 liu female 2 chen female cs 3 li male sc wang
Sample Output
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
Pre Append Code
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();
}
}
Post Append Code
答案:
package 学生大学生研究生类;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
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();
}
}
class Student
{
int no;
String name;
String gender;
public Student(int _no, String n, String g) {
this.no = _no;
this.name = n;
this.gender = g;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void print()
{
System.out.println("no: "+getNo());
System.out.println("name: "+getName());
System.out.println("sex: "+getGender());
}
}
class CollegeStudent extends Student
{
String major;
public CollegeStudent(int _no, String n, String g, String major) {
super(_no, n, g);
this.major = major;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void print()
{
super.print();
System.out.println("major: "+getMajor());
}
}
class GraduateStudent extends CollegeStudent
{
String supervisor;//导师
public GraduateStudent(int _no, String n, String g, String major, String supervisor) {
super(_no, n, g, major);
this.supervisor = supervisor;
}
public String getSupervisor() {
return supervisor;
}
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public void doResearch()
{
System.out.println(getName()+" is doing research");
}
public void print(){
super.print();
System.out.println("supervisor: "+supervisor);
}
}