Java是一種面向對象的編程語言,通過定義不同的類來實現不同的功能。在學生管理系統中,我們需要定義學生類和借書卡類,來記錄學生的基本信息和借書卡的狀態。
public class Student { private String name; private int age; private String gender; private String id; private String major; // 構造函數 public Student(String name, int age, String gender, String id, String major) { this.name = name; this.age = age; this.gender = gender; this.id = id; this.major = major; } // getter 和 setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
上面的學生類定義了學生的基本信息,通過構造函數和getter / setter 方法來實現對學生對象的初始化和訪問。
public class BorrowCard { private int cardId; private String borrowDate; private int borrowTimes; private double fine; // 構造函數 public BorrowCard(int cardId, String borrowDate, int borrowTimes, double fine) { this.cardId = cardId; this.borrowDate = borrowDate; this.borrowTimes = borrowTimes; this.fine = fine; } // getter 和 setter 方法 public int getCardId() { return cardId; } public void setCardId(int cardId) { this.cardId = cardId; } public String getBorrowDate() { return borrowDate; } public void setBorrowDate(String borrowDate) { this.borrowDate = borrowDate; } public int getBorrowTimes() { return borrowTimes; } public void setBorrowTimes(int borrowTimes) { this.borrowTimes = borrowTimes; } public double getFine() { return fine; } public void setFine(double fine) { this.fine = fine; } }
上面的借書卡類定義了借書卡的狀態信息,包括卡號、借書時間、借書次數、罰款金額。同樣通過構造函數和getter / setter 方法來實現對借書卡對象的初始化和訪問。