java创建一个student类,student类包含5个成员变量,分别为学号,姓名,性别,年龄,java成绩
时间: 2024-10-10 08:12:07 浏览: 115
在Java中,你可以创建一个名为`Student`的类来表示学生的基本信息。下面是一个简单的例子,展示了如何定义这个类及其成员变量:
```java
public class Student {
// 成员变量(属性)
private String studentID; // 学号
private String name; // 姓名
private char gender; // 性别 ('M' for Male, 'F' for Female)
private int age; // 年龄
private double grade; // Java成绩
// 构造函数(用于初始化对象)
public Student(String studentID, String name, char gender, int age, double grade) {
this.studentID = studentID;
this.name = name;
this.gender = gender;
this.age = age;
this.grade = grade;
}
// 获取和设置方法(getters和setters)
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
// 可选的toString方法,方便打印对象信息
@Override
public String toString() {
return "Student{" +
"studentID='" + studentID + '\'' +
", name='" + name + '\'' +
", gender=" + gender +
", age=" + age +
", grade=" + grade +
'}';
}
}
阅读全文