类之间最常见的关系有:
继承(“is-a”) :一个类 继承 了 另一个类。
+ 实现不易 解耦的复用 。
关联(“has-a”) :一个类 作为 另一个类的 成员变量 。
+ 实现容易 解耦的复用 。
关联:两个类关系独立、平等 。
通过 接口 或 抽象类 实现解耦 (即:动态更换 关联的对象)。
聚合:两个类关系独立、可分割的 整体与 个体 。
通过 接口 或 抽象类 实现解耦 (即:动态更换 关联的对象)。
组合:两个类关系不可分割的 整体与 部分 。
依赖(“uses-a”):一个类的方法 要 使用 或 操作 另一个类的对象。
关系的强弱程度:
实现(implements) == 泛化/继承 (Generalization) > 组合(Composition) > 聚合 (Aggregation) > 关联关系(Association) > 依赖 关系(Dependency)
类图、类关系图:
1、继承(Inheritance / Generalization) – “is-a”
定义 :子类继承 父类的属性和方法,通过 extends 实现。特点 :is-a 关系,子类是父类的一种特化 。
public class People {
private Soul soul;
private Body body;
public People ( ) {
this . soul = new Soul ( ) ;
this . body = new Body ( ) ;
}
@Override
public String toString ( ) {
return String . format ( "人由 %s 和 %s 组成" , soul. getName ( ) , body. getName ( ) ) ;
}
}
public class Driver extends People {
private Clothes clothes;
private Hat hat;
private Car car = new Car ( ) ;
public Driver ( Clothes clothes, Hat hat) {
this . clothes = clothes;
this . hat = hat;
}
public void setClothes ( Clothes clothes) {
this . clothes = clothes;
}
public void setHat ( Hat hat) {
this . hat = hat;
}
public void driveCar ( ) {
System . out. println ( String . format ( "车的型号:%s" , car. getType ( ) ) ) ;
}
@Override
public String toString ( ) {
return String . format ( "司机穿着%s和%s" , clothes. getName ( ) , hat. getName ( ) ) ;
}
}
2、实现(Realization)
定义 :类实现接口的方法,通过 implements 实现。特点 :contract( 合同、契约、协议、约定 ) 关系,类必须遵循 接口的约定 。
public interface Vehicle {
String run ( ) ;
}
public class Car implements Vehicle {
protected String type = "汽车" ;
public String getType ( ) {
return type;
}
public void add ( Oil oil) {
System . out. println ( "正在給车加" + oil. type) ;
}
@Override
public String run ( ) {
return "在行驶中" ;
}
}
3、组合(Composition)
定义 :一种强关联 ,整体控制 部分的生命周期。特点 :contains-a 关系,部分不能脱离 整体存在。
public class Body {
private String name = "身体" ;
public String getName ( ) {
return name;
}
}
public class Soul {
private String name = "灵魂" ;
public String getName ( ) {
return name;
}
}
public class People {
private Soul soul;
private Body body;
public People ( ) {
this . soul = new Soul ( ) ;
this . body = new Body ( ) ;
}
@Override
public String toString ( ) {
return String . format ( "人由 %s 和 %s 组成" , soul. getName ( ) , body. getName ( ) ) ;
}
}
4、聚合(Aggregation)-- “has-a”
定义 :一种弱关联 ,整体和部分可以独立存在 。特点 :has-a 关系,部分的生命周期不依赖于 整体。
public class Hat {
private String name = "工帽" ;
public String getName ( ) {
return name;
}
}
public class Clothes {
private String name = "工衣" ;
public String getName ( ) {
return name;
}
}
public class Driver extends People {
private Clothes clothes;
private Hat hat;
private Car car = new Car ( ) ;
public Driver ( Clothes clothes, Hat hat) {
this . clothes = clothes;
this . hat = hat;
}
public void setClothes ( Clothes clothes) {
this . clothes = clothes;
}
public void setHat ( Hat hat) {
this . hat = hat;
}
public void driveCar ( ) {
System . out. println ( String . format ( "车的型号:%s" , car. getType ( ) ) ) ;
}
@Override
public String toString ( ) {
return String . format ( "司机穿着%s和%s" , clothes. getName ( ) , hat. getName ( ) ) ;
}
}
5、关联(Association)
定义 :一个类的对象作为另一个类的成员变量 ,表示长期关系 。特点 :has-a 关系,可以是单向 或双向 。
public class Car implements Vehicle {
protected String type = "汽车" ;
public String getType ( ) {
return type;
}
public void add ( Oil oil) {
System . out. println ( "正在給车加" + oil. type) ;
}
@Override
public String run ( ) {
return "在行驶中" ;
}
}
public class Driver extends People {
private Clothes clothes;
private Hat hat;
private Car car = new Car ( ) ;
public Driver ( Clothes clothes, Hat hat) {
this . clothes = clothes;
this . hat = hat;
}
public void setClothes ( Clothes clothes) {
this . clothes = clothes;
}
public void setHat ( Hat hat) {
this . hat = hat;
}
public void driveCar ( ) {
System . out. println ( String . format ( "车的型号:%s" , car. getType ( ) ) ) ;
}
@Override
public String toString ( ) {
return String . format ( "司机穿着%s和%s" , clothes. getName ( ) , hat. getName ( ) ) ;
}
}
6、依赖(Dependency) – “uses-a”
定义 :一个类在方法中临时使用 另一个类。特点 :uses-a 关系,依赖是短暂的 。
public class Oil {
public String type = "汽油" ;
public void add ( ) { }
}
public class Car implements Vehicle {
protected String type = "汽车" ;
public String getType ( ) {
return type;
}
public void add ( Oil oil) {
System . out. println ( "正在給车加" + oil. type) ;
}
@Override
public String run ( ) {
return "在行驶中" ;
}
}