diff --git a/基础题-Shape.txt b/基础题-Shape.txt new file mode 100644 index 0000000..f12f6d9 --- /dev/null +++ b/基础题-Shape.txt @@ -0,0 +1,30 @@ +基础题—Shape类 + +class Shape { + public void draw() { + System.out.println("绘制形状"); + } +} +class Circle extends Shape { + @Override + public void draw() { + System.out.println("绘制圆形"); + } +} +class Rectangle extends Shape { + @Override + public void draw() { + System.out.println("绘制矩形"); + } +} +public class ShapeDemo { + public static void drawShape(Shape s) { + s.draw(); + } + public static void main(String[] args) { + Shape circle = new Circle(); + Shape rectangle = new Rectangle(); + drawShape(circle); + drawShape(rectangle); + } +} \ No newline at end of file diff --git a/挑战题—Person.txt b/挑战题—Person.txt new file mode 100644 index 0000000..4ff6901 --- /dev/null +++ b/挑战题—Person.txt @@ -0,0 +1,20 @@ +挑战题—Person + +public class PersonManagerTest { + public static void main(String[] args) { + PersonManager manager = new PersonManager(); + Student student1 = new Student("张三", 18, "S001", 95.5); + Student student2 = new Student("李四", 19, "S002", 88.0); + + Teacher teacher1 = new Teacher("王老师", 35, "T001", "数学"); + Teacher teacher2 = new Teacher("李老师", 40, "T002", "英语"); + + manager.addPerson(student1); + manager.addPerson(student2); + manager.addPerson(teacher1); + manager.addPerson(teacher2); + + manager.displayAllPersons(); + manager.processSpecificAttributes(); + } +} \ No newline at end of file diff --git a/进阶题-Vehicle.txt b/进阶题-Vehicle.txt new file mode 100644 index 0000000..9c93adc --- /dev/null +++ b/进阶题-Vehicle.txt @@ -0,0 +1,33 @@ +进阶题——Vehicle +abstract class Vehicle { + public abstract void run(); +} +class Car extends Vehicle { + @Override + public void run() { + System.out.println("汽车在公路上行驶"); + } +} +class Bike extends Vehicle { + @Override + public void run() { + System.out.println("自行车在道路上骑行"); + } +} +class Truck extends Vehicle { + @Override + public void run() { + System.out.println("卡车在高速公路上运输货物"); + } +} +public class VehicleDemo { + public static void main(String[] args) { + Vehicle[] vehicles = new Vehicle[3]; + vehicles[0] = new Car(); + vehicles[1] = new Bike(); + vehicles[2] = new Truck(); + for (Vehicle vehicle : vehicles) { + vehicle.run(); + } + } +} \ No newline at end of file