UML类图中的六种关系及其示例

·Association 关联

//一个类使用另一个类作为成员变量 
//实线箭头,指向被使用者(双向关联没有箭头)
//存在自身关联

示例:

class Car {
    		… …
}
class Driver {
    		private Car car = new Car();
    		public void drive() { 
… …
}
   		}
Driver -Car c +drive() : void Car

·Aggregation 聚合

//关联关系的一种,是整体和部分的关系,且部分单独于整体存在
//空心菱形实线,菱形指向整体
//聚合关系中作为成员变量的类一般使用 set 方法赋值

实例:

class Wheel{
	… …
}
class Car{
			private Wheel w;
			public void setWheel(Wheel w){
	… …
}
… …
}
Wheel Car -Wheel w +setWheel(Wheel w) : void

·Composition 组合

//关联关系的一种,是整体和部分的关系,但部分不能脱离整体存在
//实心菱形实线,菱形指向整体
实例:

class Brain{
	… …
}
class Person{
	private Brian b=new Brain();
	… …
}
Person -Brain b Brain

·Generalization 泛化(继承)

//表示子类与父类的关系
//三角箭头实线,箭头由子类指向父类
实例:

Public class Driver extends Person{
			…
}
Driver Person

·Dependency 依赖

//一个类需要使用另一个类,使用关系具有偶然性,临时性
//虚线箭头,箭头指向被使用者
实例:

public class Water{
	… …
}
public class Person{
		public void drink (Water w){
			… …
}
}
Person +drink() : void Water

·Realization 实现

//表示类实现(一个或多个)接口的关系
//三角箭头虚线,箭头由类指向接口
实例:

		public interface Animal{
	… …
}
public class Person implements Animal{
	… …
}
«interface» Animal Person

·综合示例

«interface» Animal Person -Brain b +drink() : void Water Driver -Car c +drive() : void Car -Wheel w +setWheel(Wheel w) : void Wheel Brain
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值