package schoollife;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
enum ResearchDirection { // 研究方向枚举
智慧感知, 自动驾驶, 具身智能, 多模态融合SLAM
}
enum Major{ // 专业枚举
通信工程,电子信息工程,人工智能,控制工程
}
abstract class Person // 抽象类
{
protected String name; // 姓名
protected String sex; // 性别
protected int age; // 年龄
public Person(String name, String sex, int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
abstract void work();
abstract void earn();
abstract void enjoy();
public void showInformation()
{
System.out.println("姓名:"+name+",性别:"+sex+",年龄:"+age);
}
}
class Teacher extends Person // 教师类
{
private ResearchDirection direction; // 研究方向
private String teacherID; // 工号
public Teacher(String name, String sex, int age, ResearchDirection direction, String teacherID)
{
super(name, sex, age);
this.direction = direction;
this.teacherID = teacherID;
}
ResearchDirection getDirection() {
return direction;
}
String getTeacherID() {
return teacherID;
}
void work()
{
System.out.println(name+"教导学生。");
}
void earn()
{
System.out.println(name+"领取工资。");
}
void enjoy()
{
System.out.println(name+"看电影。");
}
public void showInformation() // 方法覆盖
{
super.showInformation();
System.out.println("工号:" + teacherID+",研究方向:" + direction);
}
}
abstract class Student extends Person // 抽象类继承
{
protected String department; // 院系
protected Major major; // 专业
protected String studentID; // 学号
public Student(String name, String sex, int age, String department, Major major, String studentID)
{
super(name, sex, age);
this.department = department;
this.major = major;
this.studentID = studentID;
}
public void showInformation() // 方法覆盖
{
super.showInformation();
System.out.println("院系:" + department + ",专业:" + major + ",学号:" + studentID);
}
}
class UndergraduateStudent extends Student // 本科生类
{
public UndergraduateStudent(String name, String sex, int age, String department, Major major, String studentID)
{
super(name, sex, age, department, major, studentID);
}
void work()
{
System.out.println(name+"去教室上课。");
}
void earn()
{
System.out.println(name+"兼职赚钱。");
}
void enjoy()
{
System.out.println(name+"玩游戏。");
}
}
class GraduateStudent extends Student // 研究生类
{
private ResearchDirection direction; // 研究方向
public GraduateStudent(String name, String sex, int age, String department, Major major, String studentID,
ResearchDirection direction)
{
super(name, sex, age, department, major, studentID);
this.direction = direction;
}
ResearchDirection getDirection() {
return direction;
}
void work()
{
System.out.println(name+"做研究。");
}
void earn()
{
System.out.println(name+"领取奖学金。");
}
void enjoy()
{
System.out.println(name+"去打球。");
}
public void showInformation()
{
super.showInformation();
System.out.println("研究方向:" + direction);
}
}
public class SchoolLife {
public static void main(String[] args) {
System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8));
// 生成教师对象
Teacher[] teachers = new Teacher[3];
teachers[0] = new Teacher("张三", "男", 35, ResearchDirection.具身智能, "20001112");
teachers[1] = new Teacher("李四", "女", 42, ResearchDirection.自动驾驶, "20002234");
teachers[2] = new Teacher("王五", "男", 50, ResearchDirection.智慧感知, "20003356");
// 生成本科生对象
var undergraduate = new UndergraduateStudent("赵六", "女", 20, "电信学院", Major.通信工程, "2021123");
// 生成研究生对象
var graduate = new GraduateStudent("钱七", "男", 23, "人工智能学院", Major.控制工程,"2023123",ResearchDirection.多模态融合SLAM);
// 父类数组可以存子类对象
Person[] people = new Person[teachers.length + 2];
System.arraycopy(teachers, 0, people, 0, teachers.length);
people[teachers.length] = undergraduate;
people[teachers.length + 1] = graduate;
for(Person person:people)
{
// 类判断
if(person instanceof Teacher)
{
System.out.printf("老师");
}
else if(person instanceof UndergraduateStudent)
{
System.out.printf("本科生");
}
else if(person instanceof GraduateStudent)
{
System.out.printf("研究生");
}
//功能测试
person.showInformation();
person.work();
person.earn();
person.enjoy();
System.out.println();
}
}
}
java基础·小白入门(六)——程序编写
最新推荐文章于 2024-10-06 20:16:20 发布