java基础·小白入门(六)——程序编写

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();
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值