用Java设计一个学生类
时间: 2024-05-22 11:16:31 浏览: 74
java定义一个学生类
public class Student {
private String name; // 学生姓名
private int age; // 学生年龄
private String gender; // 学生性别
private int grade; // 学生年级
private String[] courses; // 学生选修的课程
// 构造方法
public Student(String name, int age, String gender, int grade, String[] courses) {
this.name = name;
this.age = age;
this.gender = gender;
this.grade = grade;
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 int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String[] getCourses() {
return courses;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
// 打印学生信息
public void printStudentInfo() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("性别:" + gender);
System.out.println("年级:" + grade);
System.out.print("选修课程:");
for (String course : courses) {
System.out.print(course + " ");
}
System.out.println();
}
}
阅读全文