Java是一種廣泛應用的編程語言,具有良好的面向對象特性。本文將介紹如何使用Java設計一個教師類和課程類。
教師類可包含以下屬性:
public class Teacher { private String name; //姓名 private int age; //年齡 private String gender; //性別 private String[] courses; //教授的課程 //構造函數 public Teacher(String name, int age, String gender, String[] courses) { this.name = name; this.age = age; this.gender = gender; this.courses = courses; } //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[] getCourses() { return courses; } public void setCourses(String[] courses) { this.courses = courses; } }
課程類可包含以下屬性:
public class Course { private String name; //課程名稱 private int hours; //學時 private int credit; //學分 //構造函數 public Course(String name, int hours, int credit) { this.name = name; this.hours = hours; this.credit = credit; } //getter和setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHours() { return hours; } public void setHours(int hours) { this.hours = hours; } public int getCredit() { return credit; } public void setCredit(int credit) { this.credit = credit; } }
使用Java設計教師類和課程類,可方便實現教師和課程的管理。通過定義相應的屬性和方法,可以便捷地實現相關業務邏輯。
上一篇java訓練營和正式班