用Java设计一个学生类,学生信息有身份证号,学号,专业,姓名,性别,年龄,及数学,英语,Java程序设计三门课程的成绩。创建学生类的对象并输出对象的信息。
时间: 2023-12-15 07:05:01 浏览: 79
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
下面是一个简单的Java学生类的代码实现:
```java
public class Student {
private String idCard; // 身份证号
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 idCard, String studentNo, String major, String name, String gender, int age,
double mathScore, double englishScore, double javaScore) {
this.idCard = idCard;
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 getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
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;
}
// 输出学生信息的方法
public void printInfo() {
System.out.println("姓名:" + this.name);
System.out.println("身份证号:" + this.idCard);
System.out.println("学号:" + this.studentNo);
System.out.println("专业:" + this.major);
System.out.println("性别:" + this.gender);
System.out.println("年龄:" + this.age);
System.out.println("数学成绩:" + this.mathScore);
System.out.println("英语成绩:" + this.englishScore);
System.out.println("Java程序设计成绩:" + this.javaScore);
}
}
```
然后,我们可以在主函数中创建一个学生对象,设置学生的各项信息,并输出学生的信息:
```java
public static void main(String[] args) {
Student student = new Student("123456789012345678", "18010101", "计算机科学与技术", "张三", "男", 20, 85.5, 90.0, 95.0);
student.printInfo();
}
```
输出结果如下:
```
姓名:张三
身份证号:123456789012345678
学号:18010101
专业:计算机科学与技术
性别:男
年龄:20
数学成绩:85.5
英语成绩:90.0
Java程序设计成绩:95.0
```
阅读全文