类与类之间的六大关系:泛化,实现,组合,聚合,关联,依赖。强弱关系依次递减,我们常说的降低耦合性,也就是降低类与类之间的关系。
1 泛化是一种继承关系,表示一般与特殊的关系,它指定了子类如何特化父类的所有特征和行为。
动物类:
arduino
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
猫类继承动物类,有个饲养的属性
typescript
public class Cat extends Animal{
private String breeds;
public String getBreeds() {
return breeds;
}
public Cat(String name, Integer age, String breeds) {
super(name, age);
this.breeds = breeds;
}
public void setBreeds(String breeds) {
this.breeds = breeds;
}
public void selfIntroduction(String name,Integer age,String breeds){
System.out.println("我叫"+name+",是一只"+breeds+"品种的猫,今年"+age+"岁了,");
}
}
下面我们用类图来表示这个关系:
2 实现:是一种类与接口的关系,表示类是接口所有特征和行为的实现。
先定义两个接口:
csharp
public interface Eat {
void eat();
}
csharp
public interface Sleep {
void sleep();
}
typescript
public class Animal implements Eat,Sleep {
private String name;
private int age;
public Animal(String name, int age) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void eat() {
Log.d("tanyl","吃东西");
}
@Override
public void sleep() {
Log.d("tanyl","睡觉");
}
}
用类图来表示:
3 关联:是一种拥有的关系,它使一个类知道另一个类的属性和方法,如:老师和学生,丈夫和妻子关联可以是双向的,也可以是单向的,双向的关联可以有两个箭头或者没有箭头,单向的关联有一个箭头。 现在增加一个动物出生地的地方,将这个出生地和动物关联起来。 动物出生地类:
typescript
public class BirthPlace {
private String birthplace;
public BirthPlace(String birthplace) {
this.birthplace = birthplace;
}
public String getBirthplace() {
return birthplace;
}
public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}
}
与动物类关联起来:
typescript
public class Animal implements Eat, Sleep {
private String name;
private int age;
private BirthPlace birthplace;
public Animal(String name, Integer age, BirthPlace birthplace) {
this.name = name;
this.age = age;
this.birthplace = birthplace;
}
public BirthPlace getBirthplace() {
return birthplace;
}
public Animal(String name, int age) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void eat() {
Log.d("tanyl", "吃东西");
}
@Override
public void sleep() {
Log.d("tanyl", "睡觉");
}
}
下面用类图表示一下:
4 聚合:是整体与部分的关系,且部分可以离开整体而单独存在,如车和轮胎是整体和部分的关系,轮胎离开车仍然可以存在,聚合是关联关系的一种,是强的关联关系,关联和聚合在语法上无法区分,必须考察具体的逻辑关系。 举例:每台车都会有四个轮胎和一个引擎,轮胎离开车可以单独存在,引擎同样也是。 新增一个轮子类和引擎类:
typescript
public class Wheel {
private String type;
public Wheel(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void move(){
Log.d("tanyl","滚动!!!");
}
}
typescript
public class Engine {
private String type;
public Engine(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void start() {
Log.d("tanyl", "启动引擎!!!");
}
}
汽车类
typescript
public class Car {
private Wheel wheel;
private Engine engine;
public Car(Wheel wheel, Engine engine) {
this.wheel = wheel;
this.engine = engine;
}
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public void go(){
Log.d("tanyl", "开车出去浪;啊");
}
}
下面用类图来表示一下:
5 组合:也是整体与部分的关系,但部分不能离开整体而单独存在,如公司和部门是整体和部分的关系,没有公司就不存在部门。还有比如人是由头和身体组成,没有了人,头和身体还怎么存在,组合关系是关联关系的一种,是比聚合关系还要强的关系。 先看头和身体类:
arduino
public class Body {
private double size;
public Body(double size) {
this.size = size;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
}
arduino
public class Head {
private double size;
public Head(double size) {
this.size = size;
}
public double getType() {
return size;
}
public void setType(double size) {
this.size = size;
}
}
人类:
typescript
public class Person {
private String name;
private Head head;
private Body body;
public Person(String name, Head head, Body body) {
this.name = name;
this.head = head;
this.body = body;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
public void say() {
Log.d("tanyl","我会说话");
}
}
下面看类图:
6 :依赖关系是一种使用关系,它是对象之间耦合度最弱的一种关联方式,是临时性的关联,在代码中,某个类的方法通过局部变量,方法的参数或者对静态方法的调用来访问另一个(被依赖类)中的某些方法来完成一些职责。
typescript
public class Person {
private String name;
private Head head;
private Body body;
public Person(String name, Head head, Body body) {
this.name = name;
this.head = head;
this.body = body;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
public void say() {
Log.d("tanyl","我会说话");
}
public void eat(Food food){ //这里就是依赖,人要依赖食物。
Log.d("tanyl","吃东西"+food.toString());
}
}
下面类图表示一下: