java 创建一个学生类和成绩类,包括学生的基本信息和成绩信息
时间: 2024-01-02 08:00:55 浏览: 239
学生类和成绩类是面向对象编程中常见的两个类。学生类主要包括学生的基本信息,如学号、姓名、性别、年龄等;成绩类主要包括学生的考试成绩,如语文、数学、英语等科目的成绩。
在Java中创建学生类可以使用以下代码:
```java
public class Student {
private int studentId;
private String name;
private String gender;
private int age;
// 构造方法
public Student(int studentId, String name, String gender, int age) {
this.studentId = studentId;
this.name = name;
this.gender = gender;
this.age = age;
}
// Getter和Setter方法
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
在Java中创建成绩类可以使用以下代码:
```java
public class Grade {
private int studentId;
private int chineseGrade;
private int mathGrade;
private int englishGrade;
// 构造方法
public Grade(int studentId, int chineseGrade, int mathGrade, int englishGrade) {
this.studentId = studentId;
this.chineseGrade = chineseGrade;
this.mathGrade = mathGrade;
this.englishGrade = englishGrade;
}
// Getter和Setter方法
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getChineseGrade() {
return chineseGrade;
}
public void setChineseGrade(int chineseGrade) {
this.chineseGrade = chineseGrade;
}
public int getMathGrade() {
return mathGrade;
}
public void setMathGrade(int mathGrade) {
this.mathGrade = mathGrade;
}
public int getEnglishGrade() {
return englishGrade;
}
public void setEnglishGrade(int englishGrade) {
this.englishGrade = englishGrade;
}
}
```
这样就分别创建了学生类和成绩类,可以通过实例化对象并使用Getter和Setter方法来对学生和成绩信息进行操作。
阅读全文