自身关联、合成设计模式

自身关联:
人和孩子和车的关系

class Car {
    private String name;
    private double price;
    private Person person; //车应该属于一个人
    public Car(String name,double price){
        this.name = name;
        this.price = price;
    }

    public void setPerson(Person person){
        this.person = person;
    }

    public Person getPerson(){
        return this.person;
    }

    public String getInfo(){
        return "汽车品牌:"+this.name+"价格:"+this.price;
    }
}
class Person {
    private String name;
    private int age;
    private Car car;  //一个人有一辆车
    private Person children[]; //一个人有多个孩子

    public void setChildren(Person children[]){
        this.children = children;
    }

    public Person [] getChildren(){
        return this.children;
    }

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    public void setCar(Car car){
        this.car = car;
    }

    public Car getCar(){
        return this.car;
    }

    public String getInfo() {
        return "姓名:"+this.name+"年龄:"+this.age;
    }
}
public class AVca {
    public static void main(String args[]){
        //step1:声明对象并设置彼此的关系
        Person person = new Person("临时大",29);
        Person childA = new Person("lin",18);
        Person childB = new Person("sda",19);
        childA.setCar(new Car("BWM X1",300000.00));
        childB.setCar(new Car("X5",1500000.00));
        person.setChildren(new Person [] {childA,childB});
        Car car = new Car("宾利",80000000.00);
        person.setCar(car);
        car.setPerson(person);
        //step2:根据关系获取数据
        System.out.println(person.getCar().getInfo());//查询个人的车
        System.out.println(car.getPerson().getInfo());//车撞人了,找车主
        for (int x = 0 ; x <person.getChildren().length ; x++){
            System.out.println(person.getChildren()[x].getInfo());
        }
    }
}

在这里插入图片描述

class Car {
    private String name;
    private double price;
    private Person person; //车应该属于一个人
    public Car(String name,double price){
        this.name = name;
        this.price = price;
    }

    public void setPerson(Person person){
        this.person = person;
    }

    public Person getPerson(){
        return this.person;
    }

    public String getInfo(){
        return "汽车品牌:"+this.name+"价格:"+this.price;
    }
}
class Person {
    private String name;
    private int age;
    private Car car;  //一个人有一辆车
    private Person children[]; //一个人有多个孩子

    public void setChildren(Person children[]){
        this.children = children;
    }

    public Person [] getChildren(){
        return this.children;
    }

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    public void setCar(Car car){
        this.car = car;
    }

    public Car getCar(){
        return this.car;
    }

    public String getInfo() {
        return "姓名:"+this.name+"年龄:"+this.age;
    }
}
public class AVca {
    public static void main(String args[]){
        //step1:声明对象并设置彼此的关系
        Person person = new Person("临时大",29);
        Person childA = new Person("lin",18);
        Person childB = new Person("sda",19);
        childA.setCar(new Car("BWM X1",300000.00));
        childB.setCar(new Car("X5",1500000.00));
        person.setChildren(new Person [] {childA,childB});
        Car car = new Car("宾利",80000000.00);
        person.setCar(car);
        car.setPerson(person);
        //step2:根据关系获取数据
        System.out.println(person.getCar().getInfo());//查询个人的车
        System.out.println(car.getPerson().getInfo());//车撞人了,找车主
        for (int x = 0 ; x <person.getChildren().length ; x++){
            System.out.println("\t|-" + person.getChildren()[x].getInfo());
            System.out.println("\t\t|-" + person.getChildren()[x].getCar().getInfo());
        }
    }
}

在这里插入图片描述合成设计模式:
电脑组成的类:显示器、主机
主机:主板、鼠标、键盘
主板:内存、CPU、显卡、硬盘

class 电脑 {
    private 显示器 对象数组 [] ;
    private 主机 主机 ;

}
class 显示器 {}
class 主机 {
    private 主板 对象 ;
    private 鼠标 对象 ;
    private 键盘 对象 ;
}
class 主板 {
    private 内存 对象数组 [] ;
    private CPU 对象数组 [];
    private 显卡 对象;
    private 硬盘 对象数组 [];
}
class 键盘 {}
class 鼠标 {}
class 内存 {}
class CPU {}
class 显卡 {}
class 硬盘 {}

public class AVca {
    public static void main(String args[]) {
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值