10. 类的关系——关联关系
1、类的四种关系
· 关联关系
· 依赖关系
· 继承关系
· 实现关系(本质也是继承关系)
2、关联关系
在关联关系中,没有新的语法和概念,不过是一种思想。
对象和对象之间的连接。在Java中,关联关系的代码表现形式为一个类做为另一个类的属性类型存在。即语义上是“有”的关系:”has-a”。
2.1 按整体和部分分类:
(1)组合——同生共死
(2)聚合——不同生不同死
区别:生命周期不同
例:组合——同生同死
创建整体时部分也同时创建,整体销毁部分也销毁
//组合(同生同死)
//人类
public class People {
private String name;
private String age;
private Phone[] phones;
public People() {
phones = new Phone[3];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Phone[] getPhones() {
return phones;
}
public void setPhones(Phone[] phones) {
this.phones = phones;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + ", phones=" + Arrays.toString(phones) + "]";
}
}
//电话类
public class Phone {
private String color;
private String num;
public Phone() {
// TODO Auto-generated constructor stub
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public String toString() {
return "Phone [color=" + color + ", num=" + num + "]";
}
}
//测试
public class Client {
public static void main(String[] args) {
People people = new People();
people = null;
System.out.println(people.getPhones());
}
}
结果:
例:聚合——不同死不同
创建整体时部分可以不创建,整体消亡时部分还存在。
///聚合(不同生不同死)
//人类
public class People {
private String name;
private String age;
private Phone[] phones;
public People() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Phone[] getPhones() {
return phones;
}
public void setPhones(Phone[] phones) {
this.phones = phones;
}
//电话类
public class Phone {
private String color;
private String num;
public Phone() {
// TODO Auto-generated constructor stub
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public String toString() {
return "Phone [color=" + color + ", num=" + num + "]";
}
}
//测试类
public class Client {
public Client() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
People people = new People();
Phone[] phones = new Phone[3];
people.setPhones(phones);
people = null ;
System.out.println(phones);
System.out.println(people.getPhones());
}
}
结果:
2.2 按方向分类:
(1)单向关联:A类关联B类
(2)双向关联:A关联B,B也关联A
例:单向关联
//类: 电话
public class Phone {
// 属性:型号
private String type;
// 属性:颜色
private String color;
// 属性:品牌
private String brand;
// 构造方法
public Phone() {
}
// Getters and Setters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
//类:人
public class Person {
// 属性:电话
private Phone phone;
// 构造方法:默认的
public Person() {
}
// Getters and Setters
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
}
例:双向关联
//类: 电话
public class Phone {
// 属性:型号
private String type;
// 属性:颜色
private String color;
// 属性:品牌
private String brand;
// 属性:所有者
private Person owner;
// 构造方法:默认的
public Phone() {
}
// Getters and Setters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
}
//类:人
public class Person {
private Phone phone;
public Person() {
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
}
2.3按多重性分类:
(1)一对一
(2)一对多
例:一对一
//类: 电话
public class Phone {
// 属性:型号
private String type;
// 属性:颜色
private String color;
// 属性:品牌
private String brand;
// 构造方法:默认的
public Phone() {
}
// Getters and Setters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
//类:人
public class Person {
private Phone phone;
public Person() {
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
}
例:一对多
//类: 电话
public class Phone {
// 属性:型号
private String type;
// 属性:颜色
private String color;
// 属性:品牌
private String brand;
// 构造方法
public Phone() {
}
// Getters and Setters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
//类:人
public class Person {
//数组
private Phone[] phones0 ;
//集合
private List<Phone> phones;
private Set<Phone> phones2;
private Map<String, Phone> phone3;
public Person() {
this.phones0 = new Phone[3];
phones = new ArrayList<Phone>();
}
}
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
//类:人
public class Person {
//数组
private Phone[] phones0 ;
//集合
private List<Phone> phones;
private Set<Phone> phones2;
private Map<String, Phone> phone3;
public Person() {
this.phones0 = new Phone[3];
phones = new ArrayList<Phone>();
}
}