车和人的关系:
代码实现:
class Member
{
private int mid ;
private String name ;
private Car car ;
private Member child ;
public Member(int mid, String name){
this.mid = mid ;
this.name = name ;
}
public void setCar(Car car){
this.car = car ;
}
public Car getCar(){
return this.car ;
}
public void setChild(Member child){
this.child = child ;
}
public Member getChild(){
return this.child ;
}
public String getMemberInfo(){
return "人员编号:" +this.mid+ ",姓名:"+ this.name ;
}
}
class Car
{
private String title ;
private String color ;
private Member member ;
public Car(String title,String color){
this.title = title ;
this.color = color ;
}
public void setMember(Member member)
{
this.member = member ;
}
public Member getMember(){
return this.member ;
}
public String getCarInfo(){
return "车辆信息:" +this.title+ ",车辆颜色:"+this.color ;
}
}
public class Test
{
public static void main(String args[]){
Member mem = new Member(1 , "马东") ;
Car car1 = new Car("bmw" , "红色") ;
Member chd = new Member(2 , "马丁") ;
Car car2 = new Car("奔驰", "黑色") ;
mem.setCar(car1) ; //一个人有一辆车
car1.setMember(mem) ; //一辆车对应一个人
mem.setChild(chd) ; //一个人有一个孩子
chd.setCar(car2) ; //一个孩子有一辆车
car2.setMember(chd) ; //车属于孩子
System.out.println("孩子的车:"+mem.getChild().getCar().getCarInfo()) ;
}
}