class Person{
private String name;
private int age;
private Car car;
private Person children [] ; 在人类跟车类 之后 建立CHildren属性自身关联 将孩子加入人类
public void setChildren(Person children []){
this.children=children;
}
public Person [] getChildren(){
return this.children;
}
public void setCar(Car car){
this.car=car;
}
public Car getCar(){
return car;
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
public String getInfo(){
return “人名:”+name+" 年龄:"+age;
}
}
class Car{
private String name;
private int price;
private Person person;
public void setPerson(Person person){
this.person=person;
}
public Person getPerson(){
return person;
}
public Car(String name,int price){
this.name=name;
this.price=price;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setPrice(int price){
this.price=price;
}
public int getPrice(){
return price;
}
public String getInfo(){
return “车名:”+name+" 价格:"+price;
}
}
public class javaclassplus {
public static void main(String[] args){
Person p1 =new Person("张三",18);
Person c1=new Person("李四",15); 创建人c1
Person c2=new Person("王五",16);
p1.setChildren(new Person [] {c1,c2}); 将c1 添加进Person 中的孩子属性
Car a1=new Car("bwm",2000);
Car a2=new Car("dazhong ",564151651); 建立新的车实例 a2
p1.setCar(a1);
a1.setPerson(p1);
c1.setCar(a2); 将孩子 c1 与 a2 车辆相关联
a2.setPerson(c1);
System.out.println(p1.getCar().getInfo());
System.out.println(a1.getPerson().getInfo());
System.out.println(c1.getCar().getInfo());
System.out.println(a2.getPerson().getInfo());
}
}