在一个学校中有教师和学生两类人员。学生信息有身份证号、学号、专业、龙名 性别、年龄及数学、英语、Java 程序设计三门课程的成绩。教师信息有身份证号、教师号专业、姓名、性别、年龄及授课的学时数。创建学生和教师的对象并输出对象的信息。
时间: 2023-12-23 15:02:03 浏览: 32
以下是一个简单的 Java 代码,实现创建学生和教师的对象,并输出对象信息:
```java
// 定义一个人员类
class Person {
String idCard; // 身份证号
String name; // 姓名
String gender; // 性别
int age; // 年龄
}
// 定义一个学生类,继承自 Person 类
class Student extends Person {
String stuId; // 学号
String major; // 专业
String course1; // 数学成绩
String course2; // 英语成绩
String course3; // Java 程序设计成绩
// 构造函数
public Student(String idCard, String name, String gender, int age, String stuId, String major, String course1, String course2, String course3) {
this.idCard = idCard;
this.name = name;
this.gender = gender;
this.age = age;
this.stuId = stuId;
this.major = major;
this.course1 = course1;
this.course2 = course2;
this.course3 = course3;
}
// 输出学生信息
public void printInfo() {
System.out.println("学生信息:");
System.out.println("身份证号:" + idCard);
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
System.out.println("学号:" + stuId);
System.out.println("专业:" + major);
System.out.println("数学成绩:" + course1);
System.out.println("英语成绩:" + course2);
System.out.println("Java 程序设计成绩:" + course3);
}
}
// 定义一个教师类,继承自 Person 类
class Teacher extends Person {
String teaId; // 教师号
String major; // 专业
int hours; // 授课学时数
// 构造函数
public Teacher(String idCard, String name, String gender, int age, String teaId, String major, int hours) {
this.idCard = idCard;
this.name = name;
this.gender = gender;
this.age = age;
this.teaId = teaId;
this.major = major;
this.hours = hours;
}
// 输出教师信息
public void printInfo() {
System.out.println("教师信息:");
System.out.println("身份证号:" + idCard);
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
System.out.println("教师号:" + teaId);
System.out.println("专业:" + major);
System.out.println("授课学时数:" + hours);
}
}
// 主函数
public class Main {
public static void main(String[] args) {
// 创建一个学生对象
Student stu = new Student("123456789012345678", "张三", "男", 20, "190001", "计算机科学与技术", "90", "80", "95");
// 输出学生信息
stu.printInfo();
// 创建一个教师对象
Teacher tea = new Teacher("123456789012345679", "李四", "女", 30, "001", "计算机科学与技术", 48);
// 输出教师信息
tea.printInfo();
}
}
```
输出结果如下:
```
学生信息:
身份证号:123456789012345678
姓名:张三
性别:男
年龄:20
学号:190001
专业:计算机科学与技术
数学成绩:90
英语成绩:80
Java 程序设计成绩:95
教师信息:
身份证号:123456789012345679
姓名:李四
性别:女
年龄:30
教师号:001
专业:计算机科学与技术
授课学时数:48
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)