You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

/**
* 演示:多态 —— 同一 {@link ShapeUtil#printArea(Shape)} 处理圆、矩形、三角形。
* 运行:javac *.java 后执行 java ShapeCalculatorDemo
*/
public class ShapeCalculatorDemo {
public static void main(String[] args) {
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(4.0, 5.0);
System.out.println("—— 圆 ——");
ShapeUtil.printArea(circle);
System.out.println("—— 矩形 ——");
ShapeUtil.printArea(rectangle);
System.out.println("—— 多态数组统一处理 ——");
Shape[] shapes = {circle, rectangle};
for (Shape s : shapes) {
ShapeUtil.printArea(s);
}
// 测试drawShape方法
System.out.println("\n—— 测试绘制图形 ——");
ShapeUtil.drawShape(circle);
ShapeUtil.drawShape(rectangle);
// 测试Computer和USB设备
System.out.println("\n—— 测试Computer和USB设备 ——");
Computer computer = new Computer();
Mouse mouse = new Mouse();
Keyboard keyboard = new Keyboard();
System.out.println("使用鼠标:");
computer.useUSB(mouse);
System.out.println("\n使用键盘:");
computer.useUSB(keyboard);
// 测试学生管理系统
System.out.println("\n—— 测试学生管理系统 ——");
StudentManagementSystem sms = new StudentManagementSystem();
// 创建学生对象
Student student = new Student("张三", "110101200001011234", "2024001", "计算机科学与技术");
// 创建教师对象
Teacher teacher = new Teacher("李四", "110101198001011234", "T2024001", "Java程序设计");
// 添加学生
System.out.println("添加学生:");
sms.addPerson(student);
// 添加教师
System.out.println("添加教师:");
sms.addPerson(teacher);
}
}