用Java设计一个学生类,学生信息有身份证号,学号,专业,姓名,性别,年龄及数学,英语,Java程序设计三门课程的成绩,创建学生类的对象并输出对象信息
时间: 2023-12-15 22:05:01 浏览: 70
以下是一个简单的Java学生类的示例代码:
```java
public class Student {
private String id; // 身份证号
private String studentNo; // 学号
private String major; // 专业
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private double mathScore; // 数学成绩
private double englishScore; // 英语成绩
private double javaScore; // Java程序设计成绩
// 构造方法
public Student(String id, String studentNo, String major, String name, String gender, int age, double mathScore, double englishScore, double javaScore) {
this.id = id;
this.studentNo = studentNo;
this.major = major;
this.name = name;
this.gender = gender;
this.age = age;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.javaScore = javaScore;
}
// getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
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;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
// 输出对象信息
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", studentNo='" + studentNo + '\'' +
", major='" + major + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", mathScore=" + mathScore +
", englishScore=" + englishScore +
", javaScore=" + javaScore +
'}';
}
}
```
创建学生类的对象并输出对象信息的示例代码如下:
```java
public class Main {
public static void main(String[] args) {
Student student = new Student("123456789012345678", "20210001", "计算机科学与技术", "张三", "男", 20, 80, 90, 85);
System.out.println(student);
}
}
```
输出结果如下:
```
Student{id='123456789012345678', studentNo='20210001', major='计算机科学与技术', name='张三', gender='男', age=20, mathScore=80.0, englishScore=90.0, javaScore=85.0}
```
阅读全文