UML图 类之间的关系

发布于 2020-05-08  167 次阅读


一.关联关系

代码示例
public class LoginForm{
    private JButton loginButton;
    ...
}
public class JButton{
    ...
}

双向关联

public class Customer{
private Product[] products;
...
}

public class Product{
private Customer customer;
...
}

自关联

public class Node{
private Node subNode;
...
}

多重关联

public class Form{
    private Button buttons[];
    ...
}
public class Button{
    ...
}

聚合关系

在聚合关系中,成员类是整体类的一部分,成员类可以脱离整体对象独立存在。

public class Car{
private Engine engine;
public Car(Engine engine){ this.engine = engine; }
public void setEngine(Engine engine){ this.engine = engine; }
...
}
public class Engine{
...
}

组合关系

在组合关系中,部分对象和整体对象的生命周期一致,一旦整体对象不存在,部分对象也将不存在。

public class Head{
    private Mouth mouth;
    public Head(){this.mouth = new Mouth();}
    ...
}
public class Mouth{
    ...
}

二.依赖关系

public class Driver(){
public void drive(Car car){
car.move();
}
...
}

public class Car{
public void move(){
...
}
...
}

三.泛化关系

泛化关系也就是继承关系,用来描述父类与子类间的关系。

public class Person{
    protected String name;
    protected int age;
    public void move(){...}
    public void say(){...}
}
public class Student extends Person{
    private String studentNo;
    public void study(){...}
}
public class Teacher extends Person{
    private String teacherNo;
    public void teach(){...}
}

四.接口与实现关系

public interface Vehicle(){
public void move();
}

public class Ship implements Vehicle{
public void move(){
...
}
}

public class Car implements Vehicle{
public void move(){
...
}
}


何为BS?B-big,S-shui。