代码实例:
public class Book {
private String name;
public Book() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Food {
private String name;
public Food() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class People {
private String name;
public People(String name) {
this.name = name;
}
//Dependency
public void eat(Food food){
System.out.println("eat " + food.getName());
}
//Dependency
public void read(Book book){
System.out.println("read " + book.getName());
}
}
public class Main {
public static void main(String[] args) {
People people = new People("White");
Food food = new Food();
food.setName("seafood");
Book book = new Book();
book.setName("cs");
people.eat(food);
people.read(book);
}
}
以上代码中People类依赖Food类和Book类,这种依赖关系表现为Food类和Book类作为People类中方法的参数,局部变量和静态方法调用的情况也比较简单,此处省略。
代码实例:
public class Father {
//Association
private Son son;
public Father() {
}
public void giveGift(){
System.out.println("father gives a gift to son");
}
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
}
public class Son {
//Association
private Father father;
public Son() {
}
public void getGift(){
System.out.println("son gets a gift from father");
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
}
public class Main {
public static void main(String[] args) {
Father father = new Father();
Son son = new Son();
father.setSon(son);
son.setFather(father);
father.giveGift();
son.getGift();
}
}
以上代码中Father类和Son类相互关联,各自使用对方作为自己的成员变量。
代码实例:
public class House {
private String address;
public House(String address) {
this.address = address;
}
public void getHouseAddress(){
System.out.println(this.address);
}
}
public class Car {
private String brand;
public Car(String brand) {
this.brand = brand;
}
public void getCarBrand(){
System.out.println(this.brand);
}
}
public class People {
private House house;
private Car car;
public People() {
}
public void showAssets(){
house.getHouseAddress();
car.getCarBrand();
}
public House getHouse() {
return house;
}
public void setHouse(House house) {
this.house = house;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
public class Main {
public static void main(String[] args) {
House house = new House("Central Park");
Car car = new Car("Ford");
People p = new People();
p.setCar(car);
p.setHouse(house);
p.showAssets();
}
}
以上代码中People类和House类、Car类是一个整体与个体的关系,它们都有各自的生命周期,是一种聚合关系。
代码实例:
public class Body {
public Body() {
}
public void showBody(){
System.out.println("body");
}
}
public class Soul {
public Soul() {
}
public void showSoul(){
System.out.println("soul");
}
}
public class People {
private Body body;
private Soul soul;
public People(Body body,Soul soul) {
this.body = body;
this.soul = soul;
}
public void print(){
body.showBody();
soul.showSoul();
}
}
public class Main {
public static void main(String[] args) {
People people = new People(new Body(),new Soul());
people.print();
}
}
以上代码中,People类和Body类、Soul类是整体与部分的关系,它们同生同死,不能分离单独存在。比如,这边Body和Soul都是在People的构造函数中实例化的,表示同生,且不能更换。而在前面的聚合关系中,People的House和Car是可以随时更换的。
代码实例:
public class Animal {
public Animal() {
}
public void makSound(){
System.out.println("Animal make sound!");
}
}
public class Human extends Animal{
public Human() {
}
@Override
public void makSound() {
System.out.println("Human make sound!");
}
}
public class Main {
public static void main(String[] args) {
Animal peter = new Human();
peter.makSound();
}
}
代码实例:
public interface Animal {
public void eat();
public void move();
}
public class Human implements Animal {
public Human() {
}
@Override
public void eat() {
System.out.println("Human eat...");
}
@Override
public void move() {
System.out.println("Human move...");
}
}
public class Main {
public static void main(String[] args) {
Animal mason = new Human();
mason.eat();mason.move();
}
}
结束。