【示例】模拟学生使用电脑学习
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class Computer { String brand; //品牌
} public class SxtStu { // field
int id;
String sname;
int age;
Computer comp;
void study() {
System.out.println("我正在学习!使用我们的电脑,"+comp.brand);
}
SxtStu() {
}
public static void main(String[] args) {
SxtStu stu1 = new SxtStu();
stu1.sname = "张三";
Computer comp1 = new Computer();
comp1.brand = "联想";
stu1.comp = comp1;
stu1.study();
}
} |
执行结果:
对应的UML图如下:
SxtStu和Computer的UML类图