自身关联:
人和孩子和车的关系
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[]) {
}
}