Java继承练习(3) (2024.7.9)

        学校

        Role类

package School20240709;

public class Role {
    String name;
    String age;

    public Role() {}

    public Role(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public void checkClasses() {
        System.out.println(this.name + "正在查看课表");
    }
}

        学生类

package School20240709;

public class Student extends Role{
    String classroom;

    public Student() {}

    public Student(String name, String age, String classroom) {
        super(name, age);
        this.classroom = classroom;
    }

    public String getClassroom() {
        return classroom;
    }

    public void setClassroom(String classroom) {
        this.classroom = classroom;
    }

    public void takeLesson() {
        System.out.println("学生" + this.name + "正在听课");
    }
}

        老师类

package School20240709;

public class Teacher extends Role{
    String agency;

    public Teacher() {}

    public Teacher(String name, String age, String agency) {
        super(name, age);
        this.agency = agency;
    }

    public String getAgency() {
        return agency;
    }

    public void setAgency(String agency) {
        this.agency = agency;
    }

    public void question() {
        System.out.println("老师" + this.name + "正在提问");
    }
}

        测试

package School20240709;
import Employee20240709.AdminStaff;

import java.util.Scanner;
import java.util.ArrayList;
public class SchoolTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> studentsList = new ArrayList<>();
        ArrayList<Teacher> teachersList = new ArrayList<>();
        System.out.println("请问有多少个学生要录入");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.printf("请输入第%d个学生的姓名\n", i + 1);
            String name = sc.next();
            System.out.printf("请输入第%d个学生的年龄\n", i + 1);
            String age = sc.next();
            System.out.printf("请输入第%d个学生的班级\n", i + 1);
            String classroom = sc.next();
            Student s = new Student();
            s.setName(name);
            s.setAge(age);
            s.setClassroom(classroom);
            studentsList.add(s);
            System.out.println("第" + (i + 1) + "个学生成功录入");
        }
        System.out.println("请问有多少个老师要录入");
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            System.out.printf("请输入第%d个老师的姓名\n", i + 1);
            String name = sc.next();
            System.out.printf("请输入第%d个老师的年龄\n", i + 1);
            String age = sc.next();
            System.out.printf("请输入第%d个老师的部门\n", i + 1);
            String agency = sc.next();
            Teacher t = new Teacher();
            t.setName(name);
            t.setAge(age);
            t.setAgency(agency);
            teachersList.add(t);
            System.out.println("第" + (i + 1) + "个老师成功录入");
        }
        showStudent(studentsList);
        showTeacher(teachersList);
    }
    public static void showStudent(ArrayList<Student> studentsList) {
        for (int i = 0; i < studentsList.size(); i++) {
            studentsList.get(i).takeLesson();
        }
    }

    public static void showTeacher(ArrayList<Teacher> teachersList) {
        for (int i = 0; i < teachersList.size(); i++) {
            teachersList.get(i).question();
        }
    }

}

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值