Java进阶-UML关联关系

  1. 类与类之间的连接,一个类可以知道另一个类的属性和方法,在 java 语言 中使用成员变量体现
  2. 一条直线表示
image-20200222181234031
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Me {
String name;

// 在当前类中含有其他类的引用
// 在当前对象中含有指向其他对象的引用
Friend f; // Me 和 Friend 就是关联关系

Me(Friend f) {
this.f = f;
}
}

public class Test {
public static void main(string[] args) {
Friend f = new Friend('北京');

// 建立关联关系
Me m = new Me(f);

System.out.println(m.f)
}
}

public class Friend {
String addr;
Friend(String addr) {
this.addr = addr;
}
}