以下是使用 Java 语言完成学生信息管理的代码实现:

// 人的基本类
class Person {
    private String gender;
    private String birthDate;

    public Person(String gender, String birthDate) {
        this.gender = gender;
        this.birthDate = birthDate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }
}

// 学生类
class Student extends Person {
    private String name;
    private String studentId;
    private double entranceScore;
    private String hometown;

    public Student(String gender, String birthDate, String name, String studentId, double entranceScore, String hometown) {
        super(gender, birthDate);
        this.name = name;
        this.studentId = studentId;
        this.entranceScore = entranceScore;
        this.hometown = hometown;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public double getEntranceScore() {
        return entranceScore;
    }

    public void setEntranceScore(double entranceScore) {
        this.entranceScore = entranceScore;
    }

    public String getHometown() {
        return hometown;
    }

    public void setHometown(String hometown) {
        this.hometown = hometown;
    }

    public void study() {
        System.out.println('Student ' + name + ' is studying.');
    }

    public void displayInfo() {
        System.out.println('Student Name: ' + name);
        System.out.println('Student ID: ' + studentId);
        System.out.println('Gender: ' + super.getGender());
        System.out.println('Birth Date: ' + super.getBirthDate());
        System.out.println('Entrance Score: ' + entranceScore);
        System.out.println('Hometown: ' + hometown);
    }
}

// 测试类
public class TestStudent {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print('Enter student's name: ');
        String name = scanner.nextLine();

        System.out.print('Enter student ID: ');
        String studentId = scanner.nextLine();

        System.out.print('Enter entrance score: ');
        double entranceScore = scanner.nextDouble();
        scanner.nextLine();

        System.out.print('Enter hometown: ');
        String hometown = scanner.nextLine();

        System.out.print('Enter student's gender: ');
        String gender = scanner.nextLine();

        System.out.print('Enter student's birth date: ');
        String birthDate = scanner.nextLine();

        Student student = new Student(gender, birthDate, name, studentId, entranceScore, hometown);
        student.study();
        student.displayInfo();

        scanner.close();
    }
}

在上述代码中,我们首先定义了一个基本类 Person,它包含了性别和出生日期两个数据成员,并提供了设置和获取这些属性值的方法成员。然后,我们定义了学生类 Student,它继承了 Person 类的属性,并添加了姓名、学号、入学成绩和籍贯属性。此外,我们还在 Student 类中定义了学习(study)方法和展示学生信息(displayInfo)方法。

在测试类 TestStudent 中,我们通过控制台输入学生的姓名、学号、入学成绩和籍贯等信息,并创建一个学生对象。然后调用学生对象的学习方法和展示信息方法,输出学生的相关信息。

请注意,对于性别和出生日期这两个属性,我们在创建学生对象时使用了 super 关键字来获取基类 Person 中的对应属性值。

希望这能帮助到您!

Java 学生信息管理系统:代码示例及解释

原文地址: https://www.cveoy.top/t/topic/R0d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录