實(shí)現(xiàn)關(guān)聯(lián)關(guān)系實(shí)用教案_第1頁(yè)
實(shí)現(xiàn)關(guān)聯(lián)關(guān)系實(shí)用教案_第2頁(yè)
實(shí)現(xiàn)關(guān)聯(lián)關(guān)系實(shí)用教案_第3頁(yè)
實(shí)現(xiàn)關(guān)聯(lián)關(guān)系實(shí)用教案_第4頁(yè)
實(shí)現(xiàn)關(guān)聯(lián)關(guān)系實(shí)用教案_第5頁(yè)
已閱讀5頁(yè),還剩72頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、CustomernameaddressphoneNoDockdockIDlocationelectricitywateraddSliptoDockLeaseamountstartDateendDatecalculateFee()SlipslipIDwidthslipLengthBoatstateRegistrationNoboatLengthmanufacturerYear10.10.10.11.*10.10.1類圖CoveredSlipheightdoor第2頁(yè)/共77頁(yè)第1頁(yè)/共77頁(yè)第一頁(yè),共77頁(yè)。Bradshaw類圖顯示以下關(guān)聯(lián)關(guān)系:類圖顯示以下關(guān)聯(lián)關(guān)系: 客戶客戶(k h)擁有船只

2、;擁有船只; 船只被客戶船只被客戶(k h)擁有;擁有; 船只被指定給船臺(tái);船只被指定給船臺(tái); 船臺(tái)包含船只;船臺(tái)包含船只; 碼頭包含許多船臺(tái);碼頭包含許多船臺(tái); 船臺(tái)被連接到碼頭;船臺(tái)被連接到碼頭; 船臺(tái)被租給客戶船臺(tái)被租給客戶(k h) (Lease是一個(gè)關(guān)聯(lián)類)是一個(gè)關(guān)聯(lián)類) 客戶客戶(k h)租用船臺(tái)租用船臺(tái) 第3頁(yè)/共77頁(yè)第2頁(yè)/共77頁(yè)第二頁(yè),共77頁(yè)。 船只必須被客戶擁有(BoatBoat和CustomerCustomer之間是強(qiáng)制(qingzh)(qingzh)關(guān)系) 客戶不一定擁有船只( Customer Customer和BoatBoat之間是可選關(guān)系) 使用JavaJa

3、va實(shí)現(xiàn)關(guān)聯(lián)關(guān)系的方法: 在BoatBoat類中添加屬性:CustomerCustomer類的引用變量; 在Customer Customer 類中添加屬性: Boat Boat類的引用變量;第4頁(yè)/共77頁(yè)第3頁(yè)/共77頁(yè)第三頁(yè),共77頁(yè)。修改修改(xigi)Customer(xigi)Customer類:類:public class Customerpublic class Customer / attribute definitions/ attribute definitions private String name;private String name; private Stri

4、ng address;private String address; private String phoneNo;private String phoneNo; / reference variable for Boat instance/ reference variable for Boat instanceprivate Boat boat;private Boat boat;第5頁(yè)/共77頁(yè)第4頁(yè)/共77頁(yè)第四頁(yè),共77頁(yè)。/ constructor with parameterspublic Customer(String aName, String anAddress, Stri

5、ng aPhoneNo)/ invoke accessors to populate attributessetName(aName);setAddress(anAddress);setPhoneNo(aPhoneNo);/ initially no BoatsetBoat(null); 第6頁(yè)/共77頁(yè)第5頁(yè)/共77頁(yè)第五頁(yè),共77頁(yè)。/ get accessors public String getName() return name;public String getAddress() return address;public String getPhoneNo() return ph

6、oneNo;public Boat getBoat() return boat;第7頁(yè)/共77頁(yè)第6頁(yè)/共77頁(yè)第六頁(yè),共77頁(yè)。/ set 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 = aBoa

7、t; 第8頁(yè)/共77頁(yè)第7頁(yè)/共77頁(yè)第七頁(yè),共77頁(yè)。public class TesterOneApublic static void main(String args) / create 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 first

8、Customer.setBoat(firstBoat); Boat aBoat = firstCustomer.getBoat(); System.out.println(Customer boat information is + aBoat.getStateRegistrationNo() + + aBoat.getManufacturer() + + aBoat.getLength() + + aBoat.getYear();第9頁(yè)/共77頁(yè)第8頁(yè)/共77頁(yè)第八頁(yè),共77頁(yè)。System.out.println(Again, customer boat information is +

9、firstCustomer.getBoat().getStateRegistrationNo() + + firstCustomer.getBoat().getManufacturer() + + firstCustomer.getBoat().getLength() + + firstCustomer.getBoat().getYear(); 第10頁(yè)/共77頁(yè)第9頁(yè)/共77頁(yè)第九頁(yè),共77頁(yè)。Customer boat information is MO34561 Tartan 28.0 2002Again, customer boat information is MO34561 Tar

10、tan 28.0 2002Press any key to continue.第11頁(yè)/共77頁(yè)第10頁(yè)/共77頁(yè)第十頁(yè),共77頁(yè)。修改修改Boat類實(shí)現(xiàn)另一個(gè)方向上的關(guān)系類實(shí)現(xiàn)另一個(gè)方向上的關(guān)系: 船只船只(chunzh)被客戶擁有被客戶擁有.public class Boat/ attributesprivate String stateRegistrationNo; private double length;private String manufacturer;private int year;/ reference variable points to a Customer ins

11、tanceprivate Customer customer;第12頁(yè)/共77頁(yè)第11頁(yè)/共77頁(yè)第十一頁(yè),共77頁(yè)。/ constructorpublic Boat(String aStateRegistrationNo, double aLength,String aManufacturer, int aYear)setStateRegistrationNo(aStateRegistrationNo);setLength(aLength);setManufacturer(aManufacturer);setYear(aYear);/ initially no Customer for th

12、is boatsetCustomer(null); 第13頁(yè)/共77頁(yè)第12頁(yè)/共77頁(yè)第十二頁(yè),共77頁(yè)。/ 建立兩個(gè)方向建立兩個(gè)方向(fngxing)上的關(guān)系上的關(guān)系public void assignBoatToCustomer(Customer aCustomer) setCustomer(aCustomer);/ point Boat to the Customer /instancecustomer.setBoat(this); / point Customer to this Boat第14頁(yè)/共77頁(yè)第13頁(yè)/共77頁(yè)第十三頁(yè),共77頁(yè)。/ set accessor metho

13、dspublic 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)year = aYear; public void

14、 setCustomer(Customer aCustomer)customer = aCustomer; 第15頁(yè)/共77頁(yè)第14頁(yè)/共77頁(yè)第十四頁(yè),共77頁(yè)。/ get accessor methodspublic String getStateRegistrationNo() return stateRegistrationNo; public double getLength() return length; public String getManufacturer() return manufacturer; public int getYear() return year; p

15、ublic Customer getCustomer() return customer; 第16頁(yè)/共77頁(yè)第15頁(yè)/共77頁(yè)第十五頁(yè),共77頁(yè)。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,

16、2002);/ assign the Boat to the CustomerfirstBoat.assignBoatToCustomer(firstCustomer);第17頁(yè)/共77頁(yè)第16頁(yè)/共77頁(yè)第十六頁(yè),共77頁(yè)。/ 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().getN

17、ame() + + firstBoat.getCustomer().getAddress() + + firstBoat.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() + + fi

18、rstCustomer.getBoat().getManufacturer() + + firstCustomer.getBoat().getLength() + + firstCustomer.getBoat().getYear(); 第18頁(yè)/共77頁(yè)第17頁(yè)/共77頁(yè)第十七頁(yè),共77頁(yè)。Boat owner information is Eleanor Atlanta 123-4567Customer boat information is MO34561 Tartan 28.0 2002Press any key to continue.第19頁(yè)/共77頁(yè)第18頁(yè)/共77頁(yè)第十八頁(yè),共

19、77頁(yè)。將Boat和Customer之間的關(guān)系變?yōu)閺?qiáng)制(qingzh)的,而不是可選的. 在Boat的構(gòu)造函數(shù)中增加參數(shù):Customer引用Boat構(gòu)造函數(shù)的頭為: / constructor public Boat(String aStateRegistrationNo, double aLength, String aManufacturer, int aYear, Customer aCustomer) 構(gòu)造函數(shù)中的代碼為所有的屬性設(shè)置值,然后調(diào)用assignBoatToCustomer方法,該方法會(huì)設(shè)置Boat的Customer屬性并請(qǐng)求Costomer設(shè)置其boat屬性.第20頁(yè)/

20、共77頁(yè)第19頁(yè)/共77頁(yè)第十九頁(yè),共77頁(yè)。/ constructorpublic Boat(String aStateRegistrationNo, double aLength, String aManufacturer, int aYear, Customer aCustomer)setStateRegistrationNo(aStateRegistrationNo);setLength(aLength);setManufacturer(aManufacturer);setYear(aYear);/ initially no Customer for this boat/ setCus

21、tomer(null);assignBoatToCustomer(aCustomer);第21頁(yè)/共77頁(yè)第20頁(yè)/共77頁(yè)第二十頁(yè),共77頁(yè)。增加tellAboutSelf方法:由于Boat和Customer之間的關(guān)聯(lián)是強(qiáng)制的, tellAboutSelf方法可以返回與船只有關(guān)(yugun)的信息,及與擁有該船只的客戶有關(guān)(yugun)的信息.public String tellAboutSelf()String boatDetails = I am Boat + state reg number + getStateRegistrationNo() + length + getLength

22、() + Manufacturer + getManufacturer() + Year + getYear();String customerDetails = n and Owner is + customer.getName() + living in + customer.getAddress() + with phone + customer.getPhoneNo();return boatDetails + customerDetails;第22頁(yè)/共77頁(yè)第21頁(yè)/共77頁(yè)第二十一頁(yè),共77頁(yè)。public class TesterTwopublic static void ma

23、in(String args) / create several customers Customer firstCustomer = new Customer(Eleanor, Atlanta, 123-4567); Customer secondCustomer = new Customer(JoAnn, St Louis, 987-6543); / create boats passing customer references Boat firstBoat = new Boat(MO34561, 28, Tartan, 2002, firstCustomer); Boat second

24、Boat = new Boat(MO98765, 32, Catalina, 2001, secondCustomer); / use Boat tellAboutSelf method to get back details System.out.println(firstBoat.tellAboutSelf(); System.out.println(secondBoat.tellAboutSelf(); 第23頁(yè)/共77頁(yè)第22頁(yè)/共77頁(yè)第二十二頁(yè),共77頁(yè)。I am Boatstate reg numberMO34561length28.0ManufacturerTartanYear

25、2002 and Owner is Eleanorliving in Atlantawith phone123-4567I am Boatstate reg numberMO98765length32.0ManufacturerCatalinaYear2001 and Owner is JoAnnliving in St Louiswith phone987-6543Press any key to continue.第24頁(yè)/共77頁(yè)第23頁(yè)/共77頁(yè)第二十三頁(yè),共77頁(yè)。 Slip與Dock在兩個(gè)方向上都有關(guān)聯(lián)關(guān)系:船臺(tái)被連接到碼頭,碼頭包含許多船臺(tái)。 實(shí)現(xiàn)一對(duì)多的關(guān)聯(lián)關(guān)系要求Dock實(shí)例

26、有一個(gè)以上船臺(tái)的引用變量,可以在Dock類中使用Vector保存(bocn)多個(gè)Slip類的引用變量。第25頁(yè)/共77頁(yè)第24頁(yè)/共77頁(yè)第二十四頁(yè),共77頁(yè)。DockDock類定義類定義(dngy)(dngy)屬性屬性: : ID ID號(hào)號(hào) 位置位置 (location) (location) 有電有電 (electricity) (electricity) 有水有水 (water) (water) slips slips第26頁(yè)/共77頁(yè)第25頁(yè)/共77頁(yè)第二十五頁(yè),共77頁(yè)。import java.util.*;public class Dock/ attributesprivate i

27、nt id;private String location;private boolean electricity;private boolean water;/ implement slip association with Vector classprivate Vector slips;第27頁(yè)/共77頁(yè)第26頁(yè)/共77頁(yè)第二十六頁(yè),共77頁(yè)。/ constructorpublic Dock(int anId, String aLocation, boolean anElectricity, boolean aWater)setId(anId); setLocation(aLocatio

28、n); 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)第28頁(yè)/共77頁(yè)第27頁(yè)/共77頁(yè)第二十七

29、頁(yè),共77頁(yè)。/ 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;第29頁(yè)/共77頁(yè)第28頁(yè)/共77頁(yè)第二十八頁(yè),共77頁(yè)。/ custom me

30、thod to return vector of slips public 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;第30頁(yè)/共77頁(yè)第29頁(yè)/共77頁(yè)第二十九頁(yè),共77頁(yè)。將將SlipSlip類與類與Doc

31、kDock相關(guān)聯(lián)相關(guān)聯(lián)修改修改SlipSlip類定義:類定義:增加增加DockDock引用變量引用變量(binling)(binling)作為屬性;作為屬性;修改構(gòu)造函數(shù)修改構(gòu)造函數(shù): : 增加增加DockDock引用參數(shù),這樣,實(shí)例化引用參數(shù),這樣,實(shí)例化SlipSlip時(shí),就必須與時(shí),就必須與DockDock相關(guān)聯(lián);構(gòu)造函數(shù)中的語(yǔ)相關(guān)聯(lián);構(gòu)造函數(shù)中的語(yǔ)句還會(huì)請(qǐng)求碼頭向碼頭添加船臺(tái),以便在兩個(gè)方句還會(huì)請(qǐng)求碼頭向碼頭添加船臺(tái),以便在兩個(gè)方向建立關(guān)聯(lián)關(guān)系。向建立關(guān)聯(lián)關(guān)系。第31頁(yè)/共77頁(yè)第30頁(yè)/共77頁(yè)第三十頁(yè),共77頁(yè)。/ Slip with Boat reference and acce

32、ssors/ and Dock reference variable and accessorspublic class Slip/ attributesprivate int no;private int width;private double slipLength;private Boat boat;private Dock dock;第32頁(yè)/共77頁(yè)第31頁(yè)/共77頁(yè)第三十一頁(yè),共77頁(yè)。/ constructor with 3 parameters plus dock referencepublic Slip(int aNo, int aWidth, double aSlipLen

33、gth, 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 boat in slipsetBoat(null); 第33頁(yè)/共77頁(yè)第32頁(yè)/共77頁(yè)第三十二頁(yè),共77頁(yè)。/ se

34、t 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; 第34頁(yè)/共77頁(yè)第33頁(yè)/共77頁(yè)第三十三頁(yè),共77頁(yè)。/ ge

35、t 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; 第35頁(yè)/共77頁(yè)第34頁(yè)/共77頁(yè)第三十四頁(yè),共77頁(yè)。測(cè)試測(cè)試“碼頭包含船臺(tái)碼頭包含船臺(tái)”關(guān)聯(lián)關(guān)系關(guān)聯(lián)關(guān)系聲明一個(gè)聲明一個(gè)Dock及及3個(gè)個(gè)Slip引用變量引用變量(binling),并進(jìn)行,并進(jìn)

36、行實(shí)例化。實(shí)例化。將將3個(gè)個(gè)Slip能夠與該能夠與該Dock關(guān)聯(lián),且該關(guān)聯(lián),且該Dock能夠與每個(gè)能夠與每個(gè)Slip關(guān)聯(lián)。關(guān)聯(lián)。第36頁(yè)/共77頁(yè)第35頁(yè)/共77頁(yè)第三十五頁(yè),共77頁(yè)。/ Dock has 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;

37、Slip secondSlip;Slip thirdSlip;第37頁(yè)/共77頁(yè)第36頁(yè)/共77頁(yè)第三十六頁(yè),共77頁(yè)。/ create a Dock instancefirstDock = new Dock(1, Main Cove, true, false);/ 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, firstDo

38、ck);第38頁(yè)/共77頁(yè)第37頁(yè)/共77頁(yè)第三十七頁(yè),共77頁(yè)。/ verify Dock to Slip 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 in

39、formation 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 slip information System.out.println( Slip number + aSlip.getNo() + has width of + aSlip.getWidth()+ has length of + aSlip.getSlipLength();第39

40、頁(yè)/共77頁(yè)第38頁(yè)/共77頁(yè)第三十八頁(yè),共77頁(yè)。/ verify slip to dock association (1:1)System.out.println(First slip is on Dock + firstSlip.getDock().getId()+ with location + firstSlip.getDock().getLocation()+ with electricity + firstSlip.getDock().getElectricity()+ and water + firstSlip.getDock().getWater(); 第40頁(yè)/共77頁(yè)第3

41、9頁(yè)/共77頁(yè)第三十九頁(yè),共77頁(yè)。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 location Main Cove with electricity true and waterfalsePress any key to continue.第41頁(yè)/共7

42、7頁(yè)第40頁(yè)/共77頁(yè)第四十頁(yè),共77頁(yè)。向示例中添加向示例中添加BoatBoat和和CustomerCustomer類類對(duì)對(duì)BoatBoat類進(jìn)行類進(jìn)行(jnxng)(jnxng)修改修改: : 增加增加SlipSlip引用屬性和存取器方法,以便與引用屬性和存取器方法,以便與SlipSlip關(guān)聯(lián)關(guān)聯(lián); ; 最最初不必將初不必將BoatBoat指定給指定給SlipSlip, 這樣構(gòu)造函數(shù)就可以將這樣構(gòu)造函數(shù)就可以將SlipSlip引用屬性設(shè)置為引用屬性設(shè)置為nullnull了。了。增加增加assignBoatToSlipassignBoatToSlip方法。方法。第42頁(yè)/共77頁(yè)第41頁(yè)/共

43、77頁(yè)第四十一頁(yè),共77頁(yè)。public class Boat/ attributesprivate String stateRegistrationNo; private double length;private String manufacturer;private int year;/ reference variable points to a Customer instanceprivate Customer customer;/ 增加(zngji)對(duì)Slip的引用private Slip slip;第43頁(yè)/共77頁(yè)第42頁(yè)/共77頁(yè)第四十二頁(yè),共77頁(yè)。/ constructo

44、r (adding Customer reference)public Boat(String aStateRegistrationNo, double aLength,String aManufacturer, int aYear, Customer aCustomer) setStateRegistrationNo(aStateRegistrationNo);setLength(aLength);setManufacturer(aManufacturer);setYear(aYear);/ 建立(jinl) boat 和 customer 的關(guān)聯(lián)assignBoatToCustomer(a

45、Customer);setSlip(null); / boat not in slip yet 第44頁(yè)/共77頁(yè)第43頁(yè)/共77頁(yè)第四十三頁(yè),共77頁(yè)。/ 將 Boat指定(zhdng)給 Customerpublic void assignBoatToCustomer(Customer aCustomer) setCustomer(aCustomer);customer.setBoat(this); /將 Boat指定(zhdng)到 Slippublic void assignBoatToSlip(Slip aSlip)setSlip(aSlip);slip.setBoat(this);

46、第45頁(yè)/共77頁(yè)第44頁(yè)/共77頁(yè)第四十四頁(yè),共77頁(yè)。/ tellAboutSelf method returning Boat and Customer informationpublic String tellAboutSelf()String boatDetails = I am a Boat + state reg number + getStateRegistrationNo() + length + getLength() + Manufacturer + getManufacturer() + Year + getYear(); String customerDetails

47、= n and Owner is + customer.getName() + living in + customer.getAddress() + with phone + customer.getPhoneNo();return boatDetails + customerDetails; 第46頁(yè)/共77頁(yè)第45頁(yè)/共77頁(yè)第四十五頁(yè),共77頁(yè)。/ set accessor methodspublic void setStateRegistrationNo(String aStateRegistrationNo) stateRegistrationNo = aStateRegistra

48、tionNo; 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) customer = aCustomer; public void setSlip(Slip aSlip) slip = aSlip;

49、第47頁(yè)/共77頁(yè)第46頁(yè)/共77頁(yè)第四十六頁(yè),共77頁(yè)。/ get accessor methodspublic String getStateRegistrationNo() return stateRegistrationNo; public double getLength() return length; public String getManufacturer() return manufacturer; public int getYear() return year; public Customer getCustomer() return customer; public

50、Slip getSlip() return slip; 第48頁(yè)/共77頁(yè)第47頁(yè)/共77頁(yè)第四十七頁(yè),共77頁(yè)。根據(jù)目前根據(jù)目前(mqin)的關(guān)聯(lián)關(guān)系,如果有的關(guān)聯(lián)關(guān)系,如果有Customer引用,引用,即可導(dǎo)航以查找客戶的船只、它所在的船臺(tái)及其所在即可導(dǎo)航以查找客戶的船只、它所在的船臺(tái)及其所在的碼頭;的碼頭;同樣,如果有同樣,如果有Dock引用,即可查找每個(gè)船臺(tái)并導(dǎo)航到引用,即可查找每個(gè)船臺(tái)并導(dǎo)航到船臺(tái)的船只和擁有船只的客戶。船臺(tái)的船只和擁有船只的客戶。TestThreeB提供對(duì)這些關(guān)聯(lián)關(guān)系的綜合測(cè)試。提供對(duì)這些關(guān)聯(lián)關(guān)系的綜合測(cè)試。第49頁(yè)/共77頁(yè)第48頁(yè)/共77頁(yè)第四十八頁(yè),共77頁(yè)。

51、/ TesterThreeB/ Dock has Slips 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;第50頁(yè)/共77頁(yè)第49頁(yè)

52、/共77頁(yè)第四十九頁(yè),共77頁(yè)。/ create a Dock instancefirstDock = new 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-45

53、67); secondCustomer = new Customer(JoAnn, St Louis, 987-6543); 第51頁(yè)/共77頁(yè)第50頁(yè)/共77頁(yè)第五十頁(yè),共77頁(yè)。/ 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 Slipsfirs

54、tBoat.assignBoatToSlip(firstSlip);secondBoat.assignBoatToSlip(secondSlip);/ verify Customer 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 Do

55、ck is + firstCustomer.getBoat().getSlip().getDock().getId();第52頁(yè)/共77頁(yè)第51頁(yè)/共77頁(yè)第五十一頁(yè),共77頁(yè)。/ verify Dock to Slip to 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

56、1 has + slips.size() + slips);/ iterate through Vector to get information on each Slipfor(int i = 0; i slips.size(); 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.ge

57、tSlipLength()+ n containing + aSlip.getBoat().tellAboutSelf(); 第53頁(yè)/共77頁(yè)第52頁(yè)/共77頁(yè)第五十二頁(yè),共77頁(yè)。Information for customer Eleanor Boat is Tartan Slip 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

58、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 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.

59、第54頁(yè)/共77頁(yè)第53頁(yè)/共77頁(yè)第五十三頁(yè),共77頁(yè)。 LeaseLease類層次結(jié)構(gòu)是一個(gè)關(guān)聯(lián)類,表示租約是客戶(k h)(k h)和船臺(tái)之間的關(guān)聯(lián)關(guān)系,具有起始日期、終止日期、租金等屬性。 CustomerCustomer和SlipSlip之間是一對(duì)一的關(guān)聯(lián)關(guān)系,將LeaseLease連接到此關(guān)系的虛線表示每個(gè)客戶(k (k h)h)和船臺(tái)之間有一份租約第55頁(yè)/共77頁(yè)第54頁(yè)/共77頁(yè)第五十四頁(yè),共77頁(yè)。CustomernameaddressphoneNoLeaseamountstartDateendDatecalculateFee()SlipslipIDwidthslipLen

60、gth11類圖AnnualLeasepayMonthlybalanceDuecalculateFee()DailyLeasenumberOfDayscalculateFee()第56頁(yè)/共77頁(yè)第55頁(yè)/共77頁(yè)第五十五頁(yè),共77頁(yè)。對(duì)對(duì)LeaseLease超類進(jìn)行修改:超類進(jìn)行修改:使其包括使其包括SlipSlip引用引用(ynyng)(ynyng)屬性,及屬性,及CustomerCustomer引用引用(ynyng)(ynyng)屬性。屬性。增加相應(yīng)的存取器方法。增加相應(yīng)的存取器方法。第57頁(yè)/共77頁(yè)第56頁(yè)/共77頁(yè)第五十六頁(yè),共77頁(yè)。修改(xigi)后的Lease類定義:/ Abs

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論