Java类对象的操作

2023-12-14 04:44:31

?

1.Student.java
package Message;
public class Student {
    private String stuName; // 姓名
    private int money; // 存款余额
    private String studentID; // 学号
    private int age; // 年龄
    private String address; // 地址
    private String phoneNumber; // 电话号码
    private String major; // 专业
    private String admissionYear; // 入学年份
    private boolean isRegistered; // 是否已注册
    private boolean isEnrolled; // 是否已入学
    // 构造方法
    public Student(String stuName, int money, String studentID, int age, String address, String phoneNumber,
                   String major, String admissionYear) {
        this.stuName = stuName;
        this.money = money;
        this.studentID = studentID;
        this.age = age;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.major = major;
        this.admissionYear = admissionYear;
        this.isRegistered = true;
        this.isEnrolled = true;
    }
    // Getter 和 Setter 方法
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    public String getAdmissionYear() {
        return admissionYear;
    }
    public void setAdmissionYear(String admissionYear) {
        this.admissionYear = admissionYear;
    }
    public boolean isRegistered() {
        return isRegistered;
    }
    public void setRegistered(boolean registered) {
        isRegistered = registered;
    }
    public boolean isEnrolled() {
        return isEnrolled;
    }

    public void setEnrolled(boolean enrolled) {
        isEnrolled = enrolled;
    }
    public void register() {
        isRegistered = true;
        System.out.println(stuName + "已完成报到注册");
    }
    public void payFee() {
        if (money >= 6000) {
            money -= 6000;
            System.out.println(stuName + "已支付学费,剩余存款:" + money + "元");
        } else {
            System.out.println(stuName + "的存款不足,无法支付学费。");
        }
    }
}

2.Class.java
package Message;
import java.util.ArrayList;
public class Class {
    private String className; // 班级名
    private int studentCount; // 学生人数
    private ArrayList<String> studentRoster; // 学生花名册
    private int maxCapacity; // 班级最大容量
    private String teacherName; // 班主任姓名
    // 构造方法
    public Class(String className, int maxCapacity, String teacherName) {
        this.className = className;
        this.studentCount = 0;
        this.studentRoster = new ArrayList<>();
        this.maxCapacity = maxCapacity;
        this.teacherName = teacherName;
    }
    // Getter 和 Setter 方法
    public String getClassName() {
        return className;
    }
    public void setClassName(String className) {
        this.className = className;
    }
    public int getStudentCount() {
        return studentCount;
    }
    public void setStudentCount(int studentCount) {
        this.studentCount = studentCount;
    }
    public int getMaxCapacity() {
        return maxCapacity;
    }
    public void setMaxCapacity(int maxCapacity) {
        this.maxCapacity = maxCapacity;
    }
    public String getTeacherName() {
        return teacherName;
    }
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }
    public void registerStudent(String studentName) {
        if (studentCount < maxCapacity) {
            studentCount++;
            studentRoster.add(studentName);
            System.out.println(studentName + "已注册到班级" + className);
        } else {
            System.out.println("班级" + className + "已满员,无法注册更多学生。");
        }
    }
    public void printRoster() {
        System.out.println("班级" + className + "的花名册:");
        for (String student : studentRoster) {
            System.out.println(student);
        }
    }
}

3.School.java
package Message;
public class School {
    private String universityName; // 校名
    private String universityLocation; // 校址
    private int yearFounded; // 创校年份
    private String deanName; // 校长姓名
    private int studentCapacity; // 学校容量
    // 构造方法
    public School(String universityName, String universityLocation,
                  int yearFounded, String deanName, int studentCapacity) {
        this.universityName = universityName;
        this.universityLocation = universityLocation;
        this.yearFounded = yearFounded;
        this.deanName = deanName;
        this.studentCapacity = studentCapacity;
    }
    // Getter 和 Setter 方法
    public String getUniversityName() {
        return universityName;
    }
    public void setUniversityName(String universityName) {
        this.universityName = universityName;
    }
    public String getUniversityLocation() {
        return universityLocation;
    }
    public void setUniversityLocation(String universityLocation) {
        this.universityLocation = universityLocation;
    }
    public int getYearFounded() {
        return yearFounded;
    }
    public void setYearFounded(int yearFounded) {
        this.yearFounded = yearFounded;
    }
    public String getDeanName() {
        return deanName;
    }
    public void setDeanName(String deanName) {
        this.deanName = deanName;
    }
    public int getStudentCapacity() {
        return studentCapacity;
    }

    public void setStudentCapacity(int studentCapacity) {
        this.studentCapacity = studentCapacity;
    }
}

4.StudentDemo.java
package Message;
public class StudentDemo {
        public static void main(String[] args) {
            // 创建学校对象
            School school = new School("xxx", "xxx",
                    1978, "xxx", 21000);
            // 创建班级对象
            Class classA = new Class("计算机科学与技术", 60, "王老师");
            // 创建学生对象
            Student student1 = new Student("xx", 10000, "2021",
                    20, "xxx", "18827014004", "计算机科学与技术", "2021");
            Student student2 = new Student("李华", 10000, "22006JS4204",
                    19, "xxxx", "18827014003", "计算机科学与技术", "2021");
            // 输出学校信息
            System.out.println("学校信息:");
            System.out.println("校名: " + school.getUniversityName());
            System.out.println("校址: " + school.getUniversityLocation());
            System.out.println("创校年份: " + school.getYearFounded());
            System.out.println("校长姓名: " + school.getDeanName());
            System.out.println("学校容量: " + school.getStudentCapacity());
            // 输出班级信息
            System.out.println("\n班级信息:");
            System.out.println("班级名: " + classA.getClassName());
            System.out.println("班主任姓名: " + classA.getTeacherName());
            System.out.println("班级容量: " + classA.getMaxCapacity());
            // 学生注册
            student1.register();
            student2.register();
            // 学生支付学费
            student1.payFee();
            student2.payFee();
            // 输出学生信息
            System.out.println("\n学生信息:");
            System.out.println("学生1姓名: " + student1.getStuName());
            System.out.println("学生1年龄:"+student1.getAge());
            System.out.println("学生1地址: "+student1.getAddress());
            System.out.println("学生1电话号码: "+student1.getPhoneNumber());
            System.out.println("学生1存款余额: " + student1.getMoney());
            System.out.println("学生1是否已注册: " + student1.isRegistered());
            System.out.println("学生1是否已入学: " + student1.isEnrolled());
            System.out.println("学生1专业:"+student1.getMajor());
            System.out.println("学生1学号:"+student1.getStudentID());
            System.out.println("学生1入学年份:"+student1.getAdmissionYear());
            System.out.println("\n学生2姓名: " + student2.getStuName());
            System.out.println("学生2年龄:"+student2.getAge());
            System.out.println("学生2地址: "+student2.getAddress());
            System.out.println("学生2电话号码: "+student2.getPhoneNumber());
            System.out.println("学生2存款余额: " + student2.getMoney());
            System.out.println("学生2是否已注册: " + student2.isRegistered());
            System.out.println("学生2是否已入学: " + student2.isEnrolled());
            System.out.println("学生2专业:"+student2.getMajor());
            System.out.println("学生2学号:"+student2.getStudentID());
            System.out.println("学生2入学年份:"+student2.getAdmissionYear());
        }
    }

文章来源:https://blog.csdn.net/m0_73811154/article/details/134917455
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。