Compare commits
4 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
8eafd9c74d | 1 month ago |
|
|
d71fae3ba5 | 1 month ago |
|
|
ef71cf7c3b | 1 month ago |
|
|
505430da0c | 1 month ago |
3 changed files with 216 additions and 0 deletions
@ -0,0 +1,39 @@ |
|||
// 父类 Shape |
|||
public class Shape { |
|||
public void draw() { |
|||
System.out.println("绘制一个图形"); |
|||
} |
|||
} |
|||
|
|||
// 子类 Circle |
|||
public class Circle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制一个圆形 ○"); |
|||
} |
|||
} |
|||
|
|||
// 子类 Rectangle |
|||
public class Rectangle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制一个矩形 ▭"); |
|||
} |
|||
} |
|||
|
|||
// 测试主类 |
|||
public class ShapeTest { |
|||
// 题目要求:drawShape 方法,接收 Shape 类型参数 |
|||
public static void drawShape(Shape s) { |
|||
s.draw(); // 多态调用 |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
Shape circle = new Circle(); |
|||
Shape rect = new Rectangle(); |
|||
|
|||
System.out.println("=== 基础题测试 ==="); |
|||
drawShape(circle); |
|||
drawShape(rect); |
|||
} |
|||
} |
|||
@ -0,0 +1,131 @@ |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
// 父类 Person |
|||
class Person { |
|||
private String name; |
|||
private int age; |
|||
|
|||
public Person() {} |
|||
public Person(String name, int age) { |
|||
this.name = name; |
|||
this.age = age; |
|||
} |
|||
|
|||
public void showInfo() { |
|||
System.out.println("姓名:" + name + ",年龄:" + age); |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
public void setName(String name) { |
|||
this.name = name; |
|||
} |
|||
public int getAge() { |
|||
return age; |
|||
} |
|||
public void setAge(int age) { |
|||
this.age = age; |
|||
} |
|||
} |
|||
|
|||
// 学生子类 |
|||
class Student extends Person { |
|||
private String stuId; |
|||
private String major; |
|||
|
|||
public Student() {} |
|||
public Student(String name, int age, String stuId, String major) { |
|||
super(name, age); |
|||
this.stuId = stuId; |
|||
this.major = major; |
|||
} |
|||
|
|||
@Override |
|||
public void showInfo() { |
|||
super.showInfo(); |
|||
System.out.println("身份:学生,学号:" + stuId + ",专业:" + major); |
|||
} |
|||
|
|||
public String getStuId() { |
|||
return stuId; |
|||
} |
|||
public void setStuId(String stuId) { |
|||
this.stuId = stuId; |
|||
} |
|||
public String getMajor() { |
|||
return major; |
|||
} |
|||
public void setMajor(String major) { |
|||
this.major = major; |
|||
} |
|||
} |
|||
|
|||
// 教师子类 |
|||
class Teacher extends Person { |
|||
private String teaId; |
|||
private String dept; |
|||
|
|||
public Teacher() {} |
|||
public Teacher(String name, int age, String teaId, String dept) { |
|||
super(name, age); |
|||
this.teaId = teaId; |
|||
this.dept = dept; |
|||
} |
|||
|
|||
@Override |
|||
public void showInfo() { |
|||
super.showInfo(); |
|||
System.out.println("身份:教师,工号:" + teaId + ",院系:" + dept); |
|||
} |
|||
|
|||
public String getTeaId() { |
|||
return teaId; |
|||
} |
|||
public void setTeaId(String teaId) { |
|||
this.teaId = teaId; |
|||
} |
|||
public String getDept() { |
|||
return dept; |
|||
} |
|||
public void setDept(String dept) { |
|||
this.dept = dept; |
|||
} |
|||
} |
|||
|
|||
// 管理类 |
|||
class PersonManager { |
|||
private List<Person> list = new ArrayList<>(); |
|||
|
|||
// 统一添加方法,合并addStudent、addTeacher |
|||
public void addPerson(Person p) { |
|||
list.add(p); |
|||
} |
|||
|
|||
// 遍历展示所有人信息 |
|||
public void showAll() { |
|||
for(Person p : list){ |
|||
p.showInfo(); |
|||
System.out.println("--------------------"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 主测试类 |
|||
public class PersonTest { |
|||
public static void main(String[] args) { |
|||
PersonManager manager = new PersonManager(); |
|||
|
|||
// 创建学生、教师对象 |
|||
Student s1 = new Student("张三", 19, "2025001", "大数据管理"); |
|||
Teacher t1 = new Teacher("王老师", 38, "T10086", "计算机学院"); |
|||
|
|||
// 统一添加 |
|||
manager.addPerson(s1); |
|||
manager.addPerson(t1); |
|||
|
|||
// 展示所有信息 |
|||
manager.showAll(); |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
// 抽象父类 Vehicle |
|||
public abstract class Vehicle { |
|||
public abstract void run(); |
|||
} |
|||
|
|||
// 子类 Car |
|||
public class Car extends Vehicle { |
|||
@Override |
|||
public void run() { |
|||
System.out.println("汽车在公路上行驶,速度 100km/h"); |
|||
} |
|||
} |
|||
|
|||
// 子类 Bike |
|||
public class Bike extends Vehicle { |
|||
@Override |
|||
public void run() { |
|||
System.out.println("自行车在非机动车道骑行,速度 15km/h"); |
|||
} |
|||
} |
|||
|
|||
// 子类 Truck |
|||
public class Truck extends Vehicle { |
|||
@Override |
|||
public void run() { |
|||
System.out.println("卡车在公路上缓慢行驶,速度 60km/h"); |
|||
} |
|||
} |
|||
|
|||
// 测试主类 |
|||
public class VehicleTest { |
|||
public static void main(String[] args) { |
|||
System.out.println("=== 进阶题测试 ==="); |
|||
// 创建 Vehicle 数组,存放不同车辆 |
|||
Vehicle[] vehicles = { |
|||
new Car(), |
|||
new Bike(), |
|||
new Truck() |
|||
}; |
|||
|
|||
// 遍历调用 run() 方法 |
|||
for (Vehicle v : vehicles) { |
|||
v.run(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue