归志宁无五亩园,读书本意在元元。这篇文章主要讲述思考题2(人车关系)相关的知识,希望能为你提供帮助。
车和人的关系:
【思考题2(人车关系)】代码实现:
class Memberprivate 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 Carprivate 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 Testpublic 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()) ;
推荐阅读
- static关键字
- 如何迅速清除U盘AdobeR.exe病毒?
- 新U盘存在很多不明文件怎样处理?
- U盘病毒杀不掉怎样处理?
- 用完u盘后为何要及时拔出?
- 如何完全清理u盘rose玫瑰病毒?
- u盘格式化突然中断怎样处理呢?
- 正确判断u盘中毒时所经常见的不寻常征兆
- 如何运用u盘加密器让您的u盘更加安全?