版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、课程回顾课程回顾Boat类:抽象类和最终类、重写超类方法、类:抽象类和最终类、重写超类方法、受保护访问受保护访问Lease类:抽象方法、类:抽象方法、Java接口接口n将将 Customer 与与 Boat 相关联:一对一的关相关联:一对一的关联关系联关系n向向 Boat 类中添加功能类中添加功能n将将Dock 与与 Slip 相关联:一对多的关联关系相关联:一对多的关联关系n创建和使用关联类创建和使用关联类 LeaseCustomernameaddressphoneNoDockdockIDlocationelectricitywateraddSliptoDockLeaseamountstar
2、tDateendDatecalculateFee()SlipslipIDwidthslipLengthBoatstateRegistrationNoboatLengthmanufacturerYear10.10.10.11.*10.10.1类图CoveredSlipheightdoorBradshaw类图显示以下关联关系:类图显示以下关联关系:q 客户拥有船只;客户拥有船只;q 船只被客户拥有;船只被客户拥有;q 船只被指定给船台;船只被指定给船台;q 船台包含船只;船台包含船只;q 码头包含许多船台;码头包含许多船台;q 船台被连接到码头;船台被连接到码头;q 船台被租给客户船台被租给客户
3、(Lease是一个关联类)是一个关联类)q 客户租用船台客户租用船台 l船只必须被客户拥有(船只必须被客户拥有(BoatBoat和和CustomerCustomer之间是强之间是强制关系)制关系)l客户不一定拥有船只(客户不一定拥有船只( CustomerCustomer和和BoatBoat之间是可之间是可选关系)选关系)使用使用JavaJava实现关联关系的方法:实现关联关系的方法:在在BoatBoat类中添加属性:类中添加属性:CustomerCustomer类的引用变量;类的引用变量;在在Customer Customer 类中添加属性:类中添加属性: BoatBoat类的引用变量;类的
4、引用变量;修改修改CustomerCustomer类:类:public class Customer/ attribute definitions private String name; private String address; private String phoneNo; / reference variable for Boat instanceprivate Boat boat;/ constructor with parameterspublic Customer(String aName, String anAddress, String aPhoneNo)/ invoke
5、 accessors to populate attributessetName(aName);setAddress(anAddress);setPhoneNo(aPhoneNo);/ initially no BoatsetBoat(null); / get accessors public String getName() return name;public String getAddress() return address;public String getPhoneNo() return phoneNo;public Boat getBoat() return boat;/ set
6、 accessorspublic void setName(String newName) name = newName;public void setAddress(String newAddress) address = newAddress;public void setPhoneNo(String newPhoneNo) phoneNo = newPhoneNo;public void setBoat(Boat aBoat) boat = aBoat; public class TesterOneApublic static void main(String args) / creat
7、e a Customer instance Customer firstCustomer = new Customer(Eleanor, Atlanta, 123-4567); / create a Boat instance Boat firstBoat = new Boat(MO34561, 28, Tartan, 2002); / set the Boat for the customer firstCustomer.setBoat(firstBoat); Boat aBoat = firstCustomer.getBoat(); System.out.println(Customer
8、boat information is + aBoat.getStateRegistrationNo() + + aBoat.getManufacturer() + + aBoat.getLength() + + aBoat.getYear();System.out.println(Again, customer boat information is + firstCustomer.getBoat().getStateRegistrationNo() + + firstCustomer.getBoat().getManufacturer() + + firstCustomer.getBoat
9、().getLength() + + firstCustomer.getBoat().getYear(); Customer boat information is MO34561 Tartan 28.0 2002Again, customer boat information is MO34561 Tartan 28.0 2002Press any key to continue.修改修改Boat类实现另一个方向上的关系类实现另一个方向上的关系: 船只被客户拥有船只被客户拥有.public class Boat/ attributesprivate String stateRegistrat
10、ionNo; private double length;private String manufacturer;private int year;/ reference variable points to a Customer instanceprivate Customer customer;/ constructorpublic Boat(String aStateRegistrationNo, double aLength,String aManufacturer, int aYear)setStateRegistrationNo(aStateRegistrationNo);setL
11、ength(aLength);setManufacturer(aManufacturer);setYear(aYear);/ initially no Customer for this boatsetCustomer(null); / 建立两个方向上的关系建立两个方向上的关系public void assignBoatToCustomer(Customer aCustomer) setCustomer(aCustomer);/ point Boat to the Customer /instancecustomer.setBoat(this); / point Customer to thi
12、s Boat/ set accessor methodspublic void setStateRegistrationNo(String aStateRegistrationNo) stateRegistrationNo = aStateRegistrationNo; public void setLength(double aLength) length = aLength;public void setManufacturer(String aManufacturer) manufacturer = aManufacturer; public void setYear(int aYear
13、)year = aYear; public void setCustomer(Customer aCustomer)customer = aCustomer; / get accessor methodspublic String getStateRegistrationNo() return stateRegistrationNo; public double getLength() return length; public String getManufacturer() return manufacturer; public int getYear() return year; pub
14、lic Customer getCustomer() return customer; public class TesterOneBpublic static void main(String args) / create a Customer instanceCustomer firstCustomer = new Customer(Eleanor, Atlanta, 123-4567); / create a Boat instanceBoat firstBoat = new Boat(MO34561, 28, Tartan, 2002);/ assign the Boat to the
15、 CustomerfirstBoat.assignBoatToCustomer(firstCustomer);/ verify Boat to Customer association:/ ask boat for its Customer reference / then use it with Customer accessorSystem.out.println(Boat owner information is + firstBoat.getCustomer().getName() + + firstBoat.getCustomer().getAddress() + + firstBo
16、at.getCustomer().getPhoneNo();/ verify Customer to Boat association:/ ask Customer for its Boat reference/ then use it with Boat accessorSystem.out.println(Customer boat information is + firstCustomer.getBoat().getStateRegistrationNo() + + firstCustomer.getBoat().getManufacturer() + + firstCustomer.
17、getBoat().getLength() + + firstCustomer.getBoat().getYear(); Boat owner information is Eleanor Atlanta 123-4567Customer boat information is MO34561 Tartan 28.0 2002Press any key to continue.将将Boat和和Customer之间的关系变为强制的之间的关系变为强制的,而不是可选的而不是可选的. l在在Boat的构造函数中增加参数:的构造函数中增加参数:Customer引用引用lBoat构造函数的头为构造函数的头
18、为: / constructor public Boat(String aStateRegistrationNo, double aLength, String aManufacturer, int aYear, Customer aCustomer) 构造函数中的代码为所有的属性设置值构造函数中的代码为所有的属性设置值,然后调用然后调用assignBoatToCustomer方法方法,该方法会设置该方法会设置Boat的的Customer属性并请求属性并请求Costomer设置其设置其boat属性属性./ constructorpublic Boat(String aStateRegistra
19、tionNo, double aLength, String aManufacturer, int aYear, Customer aCustomer)setStateRegistrationNo(aStateRegistrationNo);setLength(aLength);setManufacturer(aManufacturer);setYear(aYear);/ initially no Customer for this boat/ setCustomer(null);assignBoatToCustomer(aCustomer);增加tellAboutSelf方法:由于Boat和
20、Customer之间的关联是强制的, tellAboutSelf方法可以返回与船只有关的信息,及与拥有该船只的客户有关的信息.public String tellAboutSelf()String boatDetails = I am Boat + state reg number + getStateRegistrationNo() + length + getLength() + Manufacturer + getManufacturer() + Year + getYear();String customerDetails = n and Owner is + customer.get
21、Name() + living in + customer.getAddress() + with phone + customer.getPhoneNo();return boatDetails + customerDetails;public class TesterTwopublic static void main(String args) / create several customers Customer firstCustomer = new Customer(Eleanor, Atlanta, 123-4567); Customer secondCustomer = new
22、Customer(JoAnn, St Louis, 987-6543); / create boats passing customer references Boat firstBoat = new Boat(MO34561, 28, Tartan, 2002, firstCustomer); Boat secondBoat = new Boat(MO98765, 32, Catalina, 2001, secondCustomer); / use Boat tellAboutSelf method to get back details System.out.println(firstBo
23、at.tellAboutSelf(); System.out.println(secondBoat.tellAboutSelf(); I am Boatstate reg numberMO34561length28.0ManufacturerTartanYear2002 and Owner is Eleanorliving in Atlantawith phone123-4567I am Boatstate reg numberMO98765length32.0ManufacturerCatalinaYear2001 and Owner is JoAnnliving in St Louiswi
24、th phone987-6543Press any key to continue.lSlip与与Dock在两个方向上都有关联关系:船台在两个方向上都有关联关系:船台被连接到码头被连接到码头,码头包含许多船台。码头包含许多船台。l实现一对多的关联关系要求实现一对多的关联关系要求Dock实例有一个实例有一个以上船台的引用变量,可以在以上船台的引用变量,可以在Dock类中使用类中使用Vector保存多个保存多个Slip类的引用变量。类的引用变量。DockDock类定义类定义属性: ID号 位置 (location) 有电 (electricity) 有水 (water) slipsimport j
25、ava.util.*;public class Dock/ attributesprivate int id;private String location;private boolean electricity;private boolean water;/ implement slip association with Vector classprivate Vector slips;/ constructorpublic Dock(int anId, String aLocation, boolean anElectricity, boolean aWater)setId(anId);
26、setLocation(aLocation); setElectricity(anElectricity); setWater(aWater); slips = new Vector(10); / start with Vector for 10 slips/ custom method addSlipToDockpublic void addSlipToDock(Slip aSlip)slips.addElement(aSlip); / connect dock to slip (1.*) aSlip.setDock(this); / connect slip to dock (1.1)/
27、set accessor methodspublic void setId(int anId) id = anId;public void setLocation(String aLocation) location = aLocation;public void setElectricity(boolean anElectricity) electricity = anElectricity;public void setWater(boolean aWater) water = aWater;/ custom method to return vector of slips public
28、Vector getSlips() return slips;/ get accessor methodspublic int getId() return id;public String getLocation() return location;public boolean getElectricity() return electricity;public boolean getWater() return water;将将SlipSlip类与类与DockDock相关联相关联修改Slip类定义:增加Dock引用变量作为属性;(1)修改构造函数: 增加Dock引用参数,这样,实例化Sli
29、p时,就必须与Dock相关联;构造函数中的语句还会请求码头向码头添加船台,以便在两个方向建立关联关系。/ Slip with Boat reference and accessors/ and Dock reference variable and accessorspublic class Slip/ attributesprivate int no;private int width;private double slipLength;private Boat boat;private Dock dock;/ constructor with 3 parameters plus dock
30、referencepublic Slip(int aNo, int aWidth, double aSlipLength, Dock aDock) / invoke accessors to populate attributessetNo(aNo);setWidth(aWidth);setSlipLength(aSlipLength);/ assign slip to an existing docksetDock(aDock);/ tell dock to associate with this slipdock.addSlipToDock(this);/ initially no boa
31、t in slipsetBoat(null); / set accessor methods public void setNo(int aNo) no = aNo; public void setWidth(int aWidth) width = aWidth; public void setSlipLength(double aSlipLength) slipLength = aSlipLength;public void setBoat(Boat aBoat) boat = aBoat;public void setDock(Dock aDock) dock = aDock; / get
32、 accessor methodspublic int getNo() return no;public int getWidth() return width;public double getSlipLength() return slipLength;public Boat getBoat() return boat; public Dock getDock() return dock; 测试测试“码头包含船台码头包含船台”关联关系关联关系q声明一个Dock及3个Slip引用变量,并进行实例化。q将3个Slip能够与该Dock关联,且该Dock能够与每个Slip关联。/ Dock has
33、 Slips (ignores Boat and Customer for now)import java.util.*;public class TesterThreeApublic static void main(String args) / declare Dock and Slip reference variables Dock firstDock;Slip firstSlip;Slip secondSlip;Slip thirdSlip;/ create a Dock instancefirstDock = new Dock(1, Main Cove, true, false);
34、/ create three Slip instances for the DockfirstSlip = new Slip(1, 10, 20, firstDock);secondSlip = new Slip(2, 12, 25, firstDock);thirdSlip = new Slip(3, 14, 25, firstDock);/ verify Dock to Slip association (1 to many):/ first get the Vector of slips from the DockVector slips = firstDock.getSlips();/
35、 next use Vector size method to get number of slipsSystem.out.println(Dock 1 has + slips.size() + slips);/ iterate through Vector to get information on each slipfor(int i = 0; i slips.size(); i+)/ get slip reference variable from slips Vector of DockSlip aSlip = (Slip) slips.elementAt(i);/ verify sl
36、ip information System.out.println( Slip number + aSlip.getNo() + has width of + aSlip.getWidth()+ has length of + aSlip.getSlipLength();/ verify slip to dock association (1:1)System.out.println(First slip is on Dock + firstSlip.getDock().getId()+ with location + firstSlip.getDock().getLocation()+ wi
37、th electricity + firstSlip.getDock().getElectricity()+ and water + firstSlip.getDock().getWater(); Dock 1 has 3 slips Slip number 1 has width of 10 has length of 20.0 Slip number 2 has width of 12 has length of 25.0 Slip number 3 has width of 14 has length of 25.0First slip is on Dock 1 with locatio
38、n Main Cove with electricity true and waterfalsePress any key to continue.向示例中添加向示例中添加BoatBoat和和CustomerCustomer类类对Boat类进行修改: 增加Slip引用属性和存取器方法,以便与Slip关联; 最初不必将Boat指定给Slip, 这样构造函数就可以将Slip引用属性设置为null了。(1)增加assignBoatToSlip方法。public class Boat/ attributesprivate String stateRegistrationNo; private doub
39、le length;private String manufacturer;private int year;/ reference variable points to a Customer instanceprivate Customer customer;/ 增加对Slip的引用private Slip slip;/ constructor (adding Customer reference)public Boat(String aStateRegistrationNo, double aLength,String aManufacturer, int aYear, Customer
40、aCustomer) setStateRegistrationNo(aStateRegistrationNo);setLength(aLength);setManufacturer(aManufacturer);setYear(aYear);/ 建立 boat 和 customer 的关联assignBoatToCustomer(aCustomer);setSlip(null); / boat not in slip yet / 将 Boat指定给 Customerpublic void assignBoatToCustomer(Customer aCustomer) setCustomer(
41、aCustomer);customer.setBoat(this); /将 Boat指定到 Slippublic void assignBoatToSlip(Slip aSlip)setSlip(aSlip);slip.setBoat(this);/ tellAboutSelf method returning Boat and Customer informationpublic String tellAboutSelf()String boatDetails = I am a Boat + state reg number + getStateRegistrationNo() + leng
42、th + getLength() + Manufacturer + getManufacturer() + Year + getYear(); String customerDetails = n and Owner is + customer.getName() + living in + customer.getAddress() + with phone + customer.getPhoneNo();return boatDetails + customerDetails; / set accessor methodspublic void setStateRegistrationNo
43、(String aStateRegistrationNo) stateRegistrationNo = aStateRegistrationNo; public void setLength(double aLength) length = aLength;public void setManufacturer(String aManufacturer) manufacturer = aManufacturer; public void setYear(int aYear) year = aYear; public void setCustomer(Customer aCustomer) cu
44、stomer = aCustomer; public void setSlip(Slip aSlip) slip = aSlip; / get accessor methodspublic String getStateRegistrationNo() return stateRegistrationNo; public double getLength() return length; public String getManufacturer() return manufacturer; public int getYear() return year; public Customer g
45、etCustomer() return customer; public Slip getSlip() return slip; 根据目前的关联关系,如果有根据目前的关联关系,如果有Customer引用,即引用,即可导航以查找客户的船只、它所在的船台及其所在可导航以查找客户的船只、它所在的船台及其所在的码头;的码头;同样,如果有同样,如果有Dock引用,即可查找每个船台并导引用,即可查找每个船台并导航到船台的船只和拥有船只的客户。航到船台的船只和拥有船只的客户。TestThreeB提供对这些关联关系的综合测试。提供对这些关联关系的综合测试。/ TesterThreeB/ Dock has Sl
46、ips with Boat and Customerimport java.util.*;public class TesterThreeBpublic static void main(String args) / declare reference variablesDock firstDock;Slip firstSlip;Slip secondSlip;Customer firstCustomer;Customer secondCustomer;Boat firstBoat;Boat secondBoat;/ create a Dock instancefirstDock = new
47、Dock(1, Main Cove, true, false);/ create two Slip instances for the DockfirstSlip = new Slip(1, 10, 20, firstDock);secondSlip = new Slip(2, 12, 25, firstDock);/ create two Customer instances firstCustomer = new Customer(Eleanor, Atlanta, 123-4567); secondCustomer = new Customer(JoAnn, St Louis, 987-
48、6543); / create Boats passing Customer references firstBoat = new Boat(MO34561, 28, Tartan, 2002, firstCustomer);secondBoat = new Boat(MO98765, 32, Catalina, 2001, secondCustomer); / assign the Boats to the SlipsfirstBoat.assignBoatToSlip(firstSlip);secondBoat.assignBoatToSlip(secondSlip);/ verify C
49、ustomer to Boat to Slip to DockSystem.out.println(Information for customer + firstCustomer.getName()+ n Boat is + firstCustomer.getBoat().getManufacturer()+ n Slip is + firstCustomer.getBoat().getSlip().getNo()+ n Dock is + firstCustomer.getBoat().getSlip().getDock().getId();/ verify Dock to Slip to
50、 Boat association (1 to many):/ first get the Vector of slips from the DockVector slips = firstDock.getSlips();/ next use Vector size method to get number of slipsSystem.out.println(Dock 1 has + slips.size() + slips);/ iterate through Vector to get information on each Slipfor(int i = 0; i slips.size
51、(); i+)Slip aSlip = (Slip) slips.elementAt(i);/ verify Slip to Boat to Customer System.out.println( Slip number + aSlip.getNo() + has width of + aSlip.getWidth()+ has length of + aSlip.getSlipLength()+ n containing + aSlip.getBoat().tellAboutSelf(); Information for customer Eleanor Boat is Tartan Sl
52、ip is 1 Dock is 1Dock 1 has 2 slips Slip number 1 has width of 10 has length of 20.0 containing I am a Boat state reg number MO34561 length 28.0 Manufacturer Tartan Year 2002 and Owner is Eleanor living in Atlanta with phone 123-4567 Slip number 2 has width of 12 has length of 25.0 containing I am a
53、 Boat state reg number MO98765 length 32.0 Manufacturer Catalina Year 2001 and Owner is JoAnn living in St Louis with phone 987-6543Press any key to continue.lLeaseLease类层次结构是一个关联类,表示租约是类层次结构是一个关联类,表示租约是客户和船台之间的关联关系,具有起始日期、客户和船台之间的关联关系,具有起始日期、终止日期、租金等属性。终止日期、租金等属性。lCustomerCustomer和和SlipSlip之间是一对一的关
54、联关系,将之间是一对一的关联关系,将LeaseLease连接到此关系的虚线表示每个客户和船连接到此关系的虚线表示每个客户和船台之间有一份租约台之间有一份租约CustomernameaddressphoneNoLeaseamountstartDateendDatecalculateFee()SlipslipIDwidthslipLength11类图AnnualLeasepayMonthlybalanceDuecalculateFee()DailyLeasenumberOfDayscalculateFee()对对LeaseLease超类进行修改:超类进行修改: 使其包括使其包括SlipSlip引用
55、属性,及引用属性,及CustomerCustomer引用属性。引用属性。 增加相应的存取器方法。增加相应的存取器方法。修改后的Lease类定义:/ Abstact initial Lease class to extend with abstract method/ plus Slip and Customer reference attributes and accessorsimport java.util.*; / for Date and Calendar classespublic abstract class Lease/ attributesprivate double amou
56、nt;private Date startDate;private Date endDate;/ 建立对组成该Lease的Coustomer和Slip的引用 Customer customer;Slip slip;/ constructor public Lease(Date aStartDate)setStartDate(aStartDate);setEndDate(null);setAmount(0);/ no customer or slip yetsetCustomer(null);setSlip(null);/ abstract method subclasses must over
57、ridepublic abstract double calculateFee(int aLength);/ set accessor methodspublic void setAmount(double anAmount) amount = anAmount; public void setStartDate(Date aStartDate) startDate = aStartDate; public void setEndDate(Date anEndDate) endDate = anEndDate; public void setCustomer(Customer aCustome
58、r) customer = aCustomer; public void setSlip(Slip aSlip) slip = aSlip; / get accessor methodspublic double getAmount() return amount; public Date getStartDate() return startDate; public Date getEndDate() return endDate; public Customer getCustomer() return customer; public Slip getSlip() return slip
59、; q Boat不与Lease直接关联,所以类定义不必修改。q Customer类确实与Lease有关联关系,所以必须添加Lease引用属性以及存取器方法;q 对Slip类进行修改:q 添加Lease引用属性以及存取器方法;q 添加用于创建Lease实例并将它与Customer以及Slip相关联的自定义方法。由于AnnualLease和DailyLease具有包含不同参数的构造函数,这里只包含创建年租约的方法LeaseAnnualSlip.修改后的Slip类定义:import java.util.*;public class Slip/ attributesprivate int no;pri
60、vate int width;private double slipLength;private Boat boat;private Lease lease;/ constructor with 3 parameterspublic Slip(int aNo, int aWidth, double aSlipLength) / invoke accessors to populate attributessetNo(aNo);setWidth(aWidth);setSlipLength(aSlipLength);setBoat(null); / initially no boat is ass
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年库内保险柜行业直播电商战略分析研究报告
- 贵金属ETF产品设计行业2026年产业发展现状及未来发展趋势分析研究
- 企业微信营销推广合同2025
- 国有酒店管理组织结构升级成功案例|北京华恒智信
- 2006年浙江省宁波市中考数学试卷(大纲卷)【含答案】
- 音乐之声才艺展示小学主题班会课件
- 心怀感恩孝敬父母演讲稿 6篇
- 2026年中考数学真题完全解读(江西卷)
- 工程承包协议书
- 2026村书记面试题及答案详解
- GB/T 19316-2025小艇操舵轮
- 接收抵债资产管理办法
- 湖北省中小学生命安全教育课程标准(实验)
- 回收公司财务管理制度
- 房屋安全鉴定服务投标方案(技术标)
- 2025年益阳市数学五下期末学业水平测试试题含答案
- 《冰心诀》全文及解释
- 准石家庄新能力科技有限公司年产1800吨XPE发泡制品项目环境影响报告表
- JJG 455-2000工作测力仪行业标准
- 医院总值班培训课件
- 2024年青海西部机场集团青海机场有限公司招聘笔试参考题库含答案解析
评论
0/150
提交评论