diff --git a/w5/PolymorphismTest.class b/w5/PolymorphismTest.class new file mode 100644 index 0000000..baaa4c5 Binary files /dev/null and b/w5/PolymorphismTest.class differ diff --git a/w5/PolymorphismTest.java b/w5/PolymorphismTest.java new file mode 100644 index 0000000..3efbbe9 --- /dev/null +++ b/w5/PolymorphismTest.java @@ -0,0 +1,76 @@ +abstract class Person { + protected String name; + protected int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public abstract void display(); +} + +class Student extends Person { + int studentId; + String major; + + public Student(String name, int age, int studentId, String major) { + super(name, age); + this.studentId = studentId; + this.major = major; + } + + @Override + public void display() { + System.out.println("学生信息 - 姓名:" + name + ", 年龄:" + age + + ", 学号:" + studentId + ", 专业:" + major); + } +} + +class Teacher extends Person { + String subject; + double salary; + + public Teacher(String name, int age, String subject, double salary) { + super(name, age); + this.subject = subject; + this.salary = salary; + } + + @Override + public void display() { + System.out.println("教师信息 - 姓名:" + name + ", 年龄:" + age + + ", 科目:" + subject + ", 薪资:" + salary); + } +} + +public class PolymorphismTest { + public static void addPerson(Person[] array, int count, Person p) { + if (count < array.length) { + array[count] = p; + } + } + + public static void main(String[] args) { + Person[] persons = new Person[10]; + int count = 0; + + System.out.println("=== 挑战题测试 ==="); + + persons[count++] = new Student("张三", 20, 1001, "计算机科学"); + persons[count++] = new Student("李四", 22, 1002, "软件工程"); + persons[count++] = new Teacher("王老师", 35, "数据结构", 8000); + persons[count++] = new Teacher("刘老师", 40, "算法分析", 9000); + + System.out.println("添加成功!共添加 " + count + " 人\n"); + + System.out.println("=== 所有人员信息 ==="); + for (int i = 0; i < count; i++) { + persons[i].display(); + } + + System.out.println("\n=== 思考题解答 ==="); + System.out.println("多态优势:addPerson(Person p) 统一处理不同类型"); + System.out.println("添加特定属性问题:使用 instanceof 判断类型后强制转换"); + } +} \ No newline at end of file diff --git a/w5/ShapeTest.class b/w5/ShapeTest.class new file mode 100644 index 0000000..512084b Binary files /dev/null and b/w5/ShapeTest.class differ diff --git a/w5/ShapeTest.java b/w5/ShapeTest.java new file mode 100644 index 0000000..e22cd4f --- /dev/null +++ b/w5/ShapeTest.java @@ -0,0 +1,40 @@ +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 ShapeTest { + public static void drawShape(Shape s) { + s.draw(); + } + + public static void main(String[] args) { + Circle circle = new Circle(); + Rectangle rectangle = new Rectangle(); + + System.out.println("=== 基础题测试 ==="); + drawShape(circle); + drawShape(rectangle); + + Shape[] shapes = {new Circle(), new Rectangle(), new Shape()}; + System.out.println("\n遍历数组:"); + for (Shape s : shapes) { + drawShape(s); + } + } +} \ No newline at end of file diff --git a/w5/VehicleTest.java b/w5/VehicleTest.java new file mode 100644 index 0000000..f2c9360 --- /dev/null +++ b/w5/VehicleTest.java @@ -0,0 +1,37 @@ +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 VehicleTest { + public static void main(String[] args) { + Vehicle[] vehicles = {new Car(), new Bike(), new Truck()}; + + System.out.println("=== 进阶题测试 ==="); + System.out.println("车辆运行情况:"); + for (int i = 0; i < vehicles.length; i++) { + System.out.print((i + 1) + ". "); + vehicles[i].run(); + } + } +} \ No newline at end of file