题目
设计一个接口Printable,接口中有一个输出方法和输入方法;再编写一个抽象类People,类中包含姓名、性别、年龄等属性,修改和获取属性的方法。然后再编写一个学生类Student,它是People的子类同时实现了接口Printable,有自己的属性:学号、专业、成绩,实现接口中输入方法和输出方法完成学生信息的输入和输出。在测试类ks1中,创建学生类Student对象,输入输出学生信息。
测试数据:
3120150908125 张李三 男 21 软件工程 98
3120150908135 王春桥 男 22 计算机 88
3120150908126 李素华 女 20 物联网 75
3120150908455 郑春雷 女 19 信息安全 94
Printable 接口
public interface Printable {
void output();
void input();
}
People类
/**
* @Auther: 茶凡
* @ClassName People
* @Description TODO
* @date 2023/6/8 14:55
* @Version 1.0
*/
abstract class People {
private String name;
private String gender;
private int age;
public People(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public People() { }
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 int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
Student类
import java.util.Scanner;
/**
* @Auther: 茶凡
* @ClassName Student
* @Description TODO
* @date 2023/6/8 14:57
* @Version 1.0
*/
public class Student extends People implements Printable{
private String stuNo;
private String professional;
private int score; // A B C D E
public Student(String name, String gender, int age, String stuNo, String professional, int score) {
super(name, gender, age);
this.stuNo = stuNo;
this.professional = professional;
this.score = score;
}
public Student(){
super();
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getProfessional() {
return professional;
}
public void setProfessional(String professional) {
this.professional = professional;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public void output() {
System.out.println(getName() +"t" + getGender() +"t" + getAge()+"t" + stuNo+"t" + professional+"t" + score);
}
@Override
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入姓名:");
setName(scanner.next());
System.out.print("请输入性别:");
setGender(scanner.next());
System.out.print("请输入年龄:");
setAge(scanner.nextInt());
System.out.print("请输入学号:");
setStuNo(scanner.next());
System.out.print("请输入专业:");
setProfessional(scanner.next());
System.out.print("请输入成绩:");
setScore(scanner.nextInt());
}
}
测试类
/**
* @Auther: 茶凡
* @ClassName ks1
* @Description TODO
* @date 2023/6/8 15:00
* @Version 1.0
*/
public class ks1 {
public static void main(String[] args) {
Student stu1 = new Student("张李三", "男", 21, "3120150908125", "软件工程", 98);
Student stu2 = new Student("王春桥", "男", 22, "3120150908135", "计算机", 88);
Student stu3 = new Student("李素华", "女", 20, "3120150908126", "物联网", 75);
Student stu4 = new Student("郑春雷", "女", 19, "3120150908455", "信息安全", 94);
stu1.output();
stu2.output();
stu3.output();
stu4.output();
Student stu5 = new Student();
stu5.input();
stu5.output();
}
}
测试结果
张李三 男 21 3120150908125 软件工程 98
王春桥 男 22 3120150908135 计算机 88
李素华 女 20 3120150908126 物联网 75
郑春雷 女 19 3120150908455 信息安全 94
请输入姓名:胡歌
请输入性别:男
请输入年龄:40
请输入学号:199902635382
请输入专业:传媒
请输入成绩:98
胡歌 男 40 199902635382 传媒 98
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net