在一个学校中有教师和学生两类人员。学生信息有身份证号、学号、专业、姓名性别、年龄及数学、英语、Java程序设计三门课程的成绩。教师信息有身份证号、教师号、专业、姓名、性别、年龄及授课的学时数。创建学生和教师的对象并输出对象的信息。java代码
时间: 2023-09-12 11:07:11 浏览: 126
使用JSON形式创建一个对象,该对象存储一个学生的信息,该对象包含学号、身份证、年龄、性别、所学专业等属性信息,同时该对象包含一个自我介绍的方法,用来输出该对象
以下是一个简单的 Java 代码示例,用于创建学生和教师对象并输出它们的信息:
```
public class Person {
private String id;
private String name;
private String gender;
private int age;
private String major;
public Person(String id, String name, String gender, int age, String major) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.major = major;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public String getMajor() {
return major;
}
}
public class Student extends Person {
private String stuId;
private double mathScore;
private double engScore;
private double javaScore;
public Student(String id, String name, String gender, int age, String major, String stuId, double mathScore, double engScore, double javaScore) {
super(id, name, gender, age, major);
this.stuId = stuId;
this.mathScore = mathScore;
this.engScore = engScore;
this.javaScore = javaScore;
}
public String getStuId() {
return stuId;
}
public double getMathScore() {
return mathScore;
}
public double getEngScore() {
return engScore;
}
public double getJavaScore() {
return javaScore;
}
public void display() {
System.out.println("Student ID: " + stuId);
System.out.println("Name: " + getName());
System.out.println("Gender: " + getGender());
System.out.println("Age: " + getAge());
System.out.println("Major: " + getMajor());
System.out.println("Math score: " + mathScore);
System.out.println("English score: " + engScore);
System.out.println("Java score: " + javaScore);
}
}
public class Teacher extends Person {
private String teacherId;
private int teachingHours;
public Teacher(String id, String name, String gender, int age, String major, String teacherId, int teachingHours) {
super(id, name, gender, age, major);
this.teacherId = teacherId;
this.teachingHours = teachingHours;
}
public String getTeacherId() {
return teacherId;
}
public int getTeachingHours() {
return teachingHours;
}
public void display() {
System.out.println("Teacher ID: " + teacherId);
System.out.println("Name: " + getName());
System.out.println("Gender: " + getGender());
System.out.println("Age: " + getAge());
System.out.println("Major: " + getMajor());
System.out.println("Teaching hours: " + teachingHours);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("123456789", "张三", "男", 20, "计算机科学与技术", "20190101", 90.5, 85.5, 95.0);
student.display();
Teacher teacher = new Teacher("987654321", "李四", "女", 35, "计算机科学与技术", "T001", 40);
teacher.display();
}
}
```
在这个示例中,我们定义了一个 `Person` 类,它包含身份证号、姓名、性别、年龄和专业等基本信息。然后,我们定义了一个 `Student` 类和一个 `Teacher` 类,它们都继承了 `Person` 类,并添加了一些额外的信息,如学号、成绩和授课学时等。最后,在 `Main` 类中创建了一个学生对象和一个教师对象,并调用它们的 `display()` 方法输出它们的信息。
阅读全文