- 泛化关系(继承关系):继承关系可以用IS-A表示
// 父类 Animal class Animal { private String name; public Animal(String name) { this.name = name; } public void eat() { System.out.println(name + " is eating."); } } // 子类 Cat 继承自 Animal class Cat extends Animal { public Cat(String name) { super(name); } public void meow() { System.out.println("Meow!"); } }
UML类图:带空心三角形的实线表示实现关系
- 实现关系(接口实现):继承关系可以用Like-a表示
// 定义一个接口 Fruit interface Fruit { void eat(); } // 实现接口 Fruit 的类 Apple class Apple implements Fruit { @Override public void eat() { System.out.println("Eat an apple."); } }
UML类图:带空心三角形的虚线表示实现关系
-
组合关系(合成关系):整体的生命周期决定部分的生命周期,整体生命周期结束时,部分生命周期也结束,组合关系是一种特殊的聚合关系
// 引擎类 class Engine { public void start() { System.out.println("Engine started."); } public void stop() { System.out.println("Engine stopped."); } } // 汽车类 class Car { private Engine engine; public Car() { this.engine = new Engine(); } public void startCar() { engine.start(); System.out.println("Car started."); } public void stopCar() { engine.stop(); System.out.println("Car stopped."); } }
UML类图:带实心菱形和箭头的实线表示实现关系
-
聚合关系:描述整体和部分的关系,但整体的生命周期不会决定部分的生命周期,聚合关系是一种特殊的关联关系
import java.util.ArrayList; import java.util.List; // 部门类 class Department { private String name; private List<Employee> employees; public Department(String name) { this.name = name; this.employees = new ArrayList<>(); } public void addEmployee(Employee employee) { employees.add(employee); } public void removeEmployee(Employee employee) { employees.remove(employee); } public void printEmployees() { for (Employee employee : employees) { System.out.println(employee.getName()); } } } // 员工类 class Employee { private String name; public Employee(String name) { this.name = name; } public String getName() { return name; } } // 测试类 public class Main { public static void main(String[] args) { Department department = new Department("Engineering"); Employee employee1 = new Employee("John"); department.addEmployee(employee1); Employee employee2 = new Employee("Alice"); department.addEmployee(employee2); department.printEmployees(); department.removeEmployee(employee1); System.out.println("After removing employee1:"); department.printEmployees(); } }
UML类图:带空心菱形和箭头的实线表示实现关系
-
关联关系:一个类与另一个类之间有某种联系,是类与类之间的联接,它使一个类知道另一个类的属性和方法,可以用HAS-A表示。依赖关系和聚合聚合的主要区别在于,对于依赖关系来说,关系双方是平级的,是个体和个体的关系;而聚合关系是不平级的关系,是整体和部分的关系
// 学生类 class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } } // 班级类 class Class { private String className; private Student student; public Class(String className, Student student) { this.className = className; this.student = student; } public String getClassName() { return className; } public Student getStudent() { return student; } } // 测试类 public class Main { public static void main(String[] args) { Student student = new Student("Alice"); Class mathClass = new Class("Math", student); System.out.println("Student in " + mathClass.getClassName() + " class: " + mathClass.getStudent().getName()); } }
UML类图:带箭头的实线表示实现关系
-
依赖关系:依赖关系是比关联关系弱的一种关系,是所有关系中最弱的一种,体现为返回值,参数,局部变量和静态方法调用
// 依赖关系示例:汽车依赖零部件进行运行 // 零部件类 class Component { public void operation() { System.out.println("Component is performing operation."); } } // 汽车类 class Car { private Component component; public Car(Component component) { this.component = component; } public void run() { System.out.println("Car is running."); component.operation(); } } // 测试类 public class Main { public static void main(String[] args) { Component component = new Component(); Car car = new Car(component); car.run(); } }
UML类图:带箭头的虚线表示实现关系