编写问题域类定义.ppt_第1页
编写问题域类定义.ppt_第2页
编写问题域类定义.ppt_第3页
编写问题域类定义.ppt_第4页
编写问题域类定义.ppt_第5页
已阅读5页,还剩82页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第5章编写问题域类定义 课程回顾UML三层设计 第5章编写问题域类定义 开发PD类定义测试PD类编写构造函数方法编写toString方法为Slip类编写定义编写自定义方法格式化输出使用静态变量和静态方法重载方法处理异常 第5章编写问题域类定义 编写规范类名 大写字母开头 Customer属性名 小写字母开头 但后面的字可用大写字母 phoneNo方法名 动词在前 名次在后 小写字母开头 setAddress 第5章编写问题域类定义 首先以客户类为例子定义Customer类定义属性 privateStringname privateStringaddress privateStringphoneNo 定义方法 get方法 set方法 5 1开发PD类定义 5 1开发PD类定义 Java类的结构定义 publicclassCustomer attributedefinitionsprivateStringname privateStringaddress privateStringphoneNo get方法publicStringgetName returnname publicStringgetAddress returnaddress publicStringgetPhoneNo returnphoneNo 5 1开发PD类定义 set方法publicvoidsetName StringnewName name newName publicvoidsetAddress StringnewAddress address newAddress publicvoidsetPhoneNo StringnewPhoneNo phoneNo newPhoneNo 5 1开发PD类定义 5 1开发PD类定义 publicclassTesterOne publicstaticvoidmain Stringargs CustomerfirstCustomer newCustomer createinstance invokesetaccessorstopopulateattributesfirstCustomer setName Eleanor firstCustomer setAddress Atlanta firstCustomer setPhoneNo 123 4567 definevariablestocontainattributevaluesretrievedStringcustomerName customerAddress customerPhoneNo 5 2测试PD类 invokegetaccessorstoretrieveattributevaluescustomerName firstCustomer getName customerAddress firstCustomer getAddress customerPhoneNo firstCustomer getPhoneNo displaytheretrievedattributevaluesSystem out println Thenameis customerName System out println Theaddressis customerAddress System out println Thephoneis customerPhoneNo 5 2测试PD类 TesterOne序列图 5 2测试PD类 创建多个实例 5 2测试PD类 构造方法 默认的构造方法 自定义构造方法 默认的构造方法仅由头和一个空的代码块组成 PublicCustomer 5 3编写构造方法 自定义构造函数 参数化的构造函数 publicCustomer StringaName StringanAddress StringaPhoneNo invokesetterstopopulateattributesthis name aName address anAddress phoneNo aPhoneNo 5 3编写构造方法 创建多个实例 姓名 EleanorMikeJoAnn地址 AtlantaBostonSt Louis电话号码 123 4567456 1234765 4321 5 3编写构造方法 编写TesterThree类对Customer类中的构造函数方法进行测试 publicclassTesterThree publicstaticvoidmain Stringargs definethereferencevariablesCustomerfirstCustomer secondCustomer thirdCustomer createthreeCustomerinstanceswithattributevaluesfirstCustomer newCustomer Eleanor Atlanta 123 4567 secondCustomer newCustomer Mike Boston 467 1234 thirdCustomer newCustomer JoAnn St Louis 765 4321 retrieve 5 3编写构造方法 作用 检索实例所有的属性值 将它们置于一个String对象中 publicStringtoString Stringinfo info Customername getName Address getAddress PhoneNumber getPhoneNo returninfo 5 4编写toString方法 publicclassTesterFour publicstaticvoidmain Stringargs definethereferencevariablesCustomerfirstCustomer secondCustomer thirdCustomer createthreeCustomerinstanceswithattributevaluesfirstCustomer newCustomer Eleanor Atlanta 123 4567 secondCustomer newCustomer Mike Boston 467 1234 thirdCustomer newCustomer JoAnn St Louis 765 4321 invoketellAbutSelfforallthreecustomers 5 4编写toString方法 SlipslipIdwidthslipLength 5 5为Slip类编写定义 Slip类的类图 publicclassSlip attributesprivateintslipId privateintwidth privatedoubleslipLength constructorwith3parameterspublicSlip intanId intaWidth doubleaSlipLength invokesetterstopopulateattributessetSlipId anId setWidth aWidth setSlipLength aSlipLength 5 5为Slip类编写定义 setmethodspublicvoidsetSlipId intanId slipId anId publicvoidsetWidth intaWidth width aWidth publicvoidsetSlipLength doubleaSlipLength slipLength aSlipLength getmethodspublicintgetSlipId returnslipId publicintgetWidth returnwidth publicdoublegetSlipLength returnslipLength 5 5为Slip类编写定义 custommethodtoStringpublicStringtoString Stringinfo info Slip Id getSlipId Width getWidth Length getSlipLength returninfo 5 5为Slip类编写定义 编写tester类 TestOne 来测试代码的正确性 TestOne类仅有一个单一方法main 在mian中定义具有三个元素的数组slips来保存Slip实例 Slipslips newSlip 3 构造函数的三个参数分别表示slipId width slipLength Slips 0 newSlip 1 10 20 Slips 1 newSlip 2 12 25 Slips 2 newSlip 3 14 30 5 5为Slip类编写定义 publicclassTesterOne publicstaticvoidmain Stringargs createanarraytoholdthreeSlipreferencesSlipslips newSlip 3 create3Slipinstancesslips 0 newSlip 1 10 20 slips 1 newSlip 2 12 25 slips 2 newSlip 3 14 30 retrieve 5 5为Slip类编写定义 5 5为Slip类编写定义 Slip Id 1 Width 10 Length 20 0Slip Id 2 Width 12 Length 25 0Slip Id 3 Width 14 Length 30 0Pressanykeytocontinue 5 5为Slip类编写定义 编写自定义方法leaseSlip 计算船台的出租费用 出租费用 船台宽度年租金10 80012 90014 110016 1500 5 6编写自定义方法 custommethodtoleaseaSlippublicdoubleleaseSlip doublefee switch width case10 fee 800 break case12 fee 900 break case14 fee 1100 break case16 fee 1500 break default fee 0 returnfee 5 6编写自定义方法 publicclassTesterTwo publicstaticvoidmain Stringargs createanarraytoholdthreeSlipreferencesSlipslips newSlip 3 create3Slipinstancesslips 0 newSlip 1 10 20 slips 1 newSlip 2 12 25 slips 2 newSlip 3 14 30 compute 5 6编写自定义方法 Feeis800 0forslip1Feeis900 0forslip2Feeis1100 0forslip3Pressanykeytocontinue 5 6编写自定义方法 使用NumberFormat和DecimalFormat 在java text包中 NumberFormat类提供将数字数据作为货币 带有逗号 货币符号和小数点 进行格式化的方法 以及不同国家格式化货币的方法 使用DecimalFormat类对带有逗号和小数点 但未带有美元符号的数字进行格式化 5 7格式化输出 ClassNumberFormatjava lang Object java text Format 在使用NumberFormat类之前 需要使用import语句 importjava text java text NumberFormat 5 7格式化输出 publicabstractclassNumberFormatextendsFormat静态方法 getInstancepublicstaticfinalNumberFormatgetInstance 5 7格式化输出 使用getInstance方法 格式化数据 NumberFormatgeneral NumberFormat getInstance myString general format myNumber 格式化多个数据 NumberFormatnf NumberFormat getInstance for inti 0 i a length i System out println nf format myNumber i 5 7格式化输出 importjava text publicclassTesterThree publicstaticvoidmain Stringargs createaSlipinstanceSlipaSlip newSlip 3 14 30 computeleasefeedoublefee aSlip leaseSlip System out println fee illustratenumberformatNumberFormatnf NumberFormat getInstance System out println number nf format fee 5 7格式化输出 1100 0number 1 100Pressanykeytocontinue 5 7格式化输出 getCurrencyInstance方法 publicstaticfinalNumberFormatgetCurrencyInstance Returnsacurrencyformatforthecurrentdefaultlocale publicstaticNumberFormatgetCurrencyInstance LocaleinLocale Returnsacurrencyformatforthespecifiedlocale 5 7格式化输出 importjava text importjava util publicclassTesterThree publicstaticvoidmain Stringargs createaSlipinstanceSlipaSlip newSlip 3 14 30 computeleasefeedoublefee aSlip leaseSlip illustratecurrencyformatNumberFormatcurrencyFormat NumberFormat getCurrencyInstance System out println Currency currencyFormat format fee NumberFormatccFormat NumberFormat getCurrencyInstance Locale US System out println Currency ccFormat format fee 5 7格式化输出 Currency 1 100 00Currency 1 100 00Pressanykeytocontinue 5 7格式化输出 java textClassDecimalFormatjava lang Object java text Format java text NumberFormat java text DecimalFormat 5 7格式化输出 publicclassDecimalFormatextendsNumberFormat DecimalFormatisaconcretesubclassofNumberFormatthatformatsdecimalnumbers Italsosupportsdifferentkindsofnumbers includingintegers 123 fixed pointnumbers 123 4 scientificnotation 1 23E4 percentages 12 5 7格式化输出 DecimalFormatd1Format newDecimalFormat 0 00 要显示的数字保留位置 会取消所有的前导零0强制显示某个字符 即使字符的值为零 5 7格式化输出 importjava text importjava util publicclassTesterThree publicstaticvoidmain Stringargs createaSlipinstanceSlipaSlip newSlip 3 14 30 doublefee aSlip leaseSlip DecimalFormatd1Format newDecimalFormat 0 00 System out println Decimal d1Format format fee DecimalFormatd2Format newDecimalFormat 0 00 System out println Decimal d2Format format fee DecimalFormatd3Format newDecimalFormat 00000 00 System out println Decimal d3Format format fee 5 7格式化输出 Decimal 1 100 00Decimal 11 00 00Decimal 01100 00Pressanykeytocontinue 5 7格式化输出 使用换码顺序 n newline b backspace f formfeed singlequote t tab r return backslash doublequote 5 7格式化输出 importjava text publicclassTesterThree publicstaticvoidmain Stringargs createaSlipinstanceSlipaSlip newSlip 3 14 30 computeleasefeedoublefee aSlip leaseSlip illustrateescapesequencesSystem out println Beforetab taftertab System out println Beforenewline nafternewline System out println Displaydoublequote 5 7格式化输出 BeforetabaftertabBeforenewlineafternewlineDisplaydoublequote Pressanykeytocontinue 5 7格式化输出 静态变量 类变量 静态方法 类方法 关键字 static所有实例共享 每个实例没有自己的副本 5 8使用静态变量和静态方法 在Slip类中加入静态变量船台数量 privatestaticintnumberofSlips 0 在Slip类的构造函数中增加下面的语句 numberofSlips 编写getter方法返回numberofSlips的内容 publicstaticintgetNumberofSlips returngetNumberofSlips 5 8使用静态变量和静态方法 publicclassSlip attributesprivateintslipId privateintwidth privatedoubleslipLength staticattributevariableprivatestaticintnumberOfSlips 0 constructorwith3parameterspublicSlip intanId intaWidth doubleaSlipLength invokesetterstopopulateattributessetSlipId anId setWidth aWidth setSlipLength aSlipLength numberOfSlips 5 8使用静态变量和静态方法 custommethodtoleaseaSlippublicdoubleleaseSlip doublefee switch width case10 fee 800 break case12 fee 900 break case14 fee 1100 break case16 fee 1500 break default fee 0 returnfee 5 8使用静态变量和静态方法 setterspublicvoidsetSlipId intanId slipId anId publicvoidsetWidth intaWidth width aWidth publicvoidsetSlipLength doubleaSlipLength slipLength aSlipLength getterspublicintgetSlipId returnslipId publicintgetWidth returnwidth publicdoublegetSlipLength returnslipLength staticmethodpublicstaticintgetNumberOfSlips returnnumberOfSlips 5 8使用静态变量和静态方法 toStringpublicStringtoString Stringinfo info Slip Id getSlipId Width getWidth Length getSlipLength returninfo 5 8使用静态变量和静态方法 publicclassTesterFour publicstaticvoidmain Stringargs Slipslips newSlip 3 slips 0 newSlip 1 10 20 System out println Numberofslips Slip getNumberOfSlips slips 1 newSlip 2 12 25 System out println Numberofslips Slip getNumberOfSlips slips 2 newSlip 3 14 30 System out println Numberofslips Slip getNumberOfSlips retrieve 5 8使用静态变量和静态方法 Numberofslips1Numberofslips2Numberofslips3Numberofslips refvar 3Pressanykeytocontinue 5 8使用静态变量和静态方法 方法签名 methodsignature 包括方法名及参数表Java通过签名来识别方法 方法重载 overloadedmethod 在一个类中可以定义多个方法 它们具有相同的方法名及不同的参数表 5 9重载方法 与多态的区别 多态 当一个类中的某方法与第二个类中的某方法具有相同签名重载 同一个类中有两个以上相同的方法名 但是参数表不同 5 9重载方法 重载构造函数 有时需要多个构造函数 每一个构造函数有不同的参数表 大多数船台有12英尺宽 25英尺长 但少数及个船台具有不同的宽度和长度 目前的Slip构造函数需要3个实参 slipId width slipLength 5 9重载方法 改进 增加第二个构造函数 只有slipId一个形参 另外两项使用默认值 12feetwideand25feetlong 5 9重载方法 此外 为了方便以后代码维护 设置静态常量privatestaticfinalintDEFAULT WIDTH 12 privatestaticfinalintDEFAULT SLIP LENGTH 25 5 9重载方法 第二个构造函数方法除了只有一个形参用来接收船台ID值 publicSlip intanId invoke3 parameterconstructorwithdefaultwidth 5 9重载方法 publicclassTesterFive publicstaticvoidmain Stringargs create2Slipinstancesusingdifferentconstructors firstuseoriginalconstructorpassing3argumentsSlipfirstSlip newSlip 1 10 20 next use2ndconstructorpassingonly1argumentSlipsecondSlip newSlip 2 retrieve 5 9重载方法 Slip Id 1 Width 10 Length 20 0Slip Id 2 Width 12 Length 25 0 5 9重载方法 重载自定义方法 如在特殊情况下 允许将租金打折 编写leaseSlip方法的第二个版本 接受百分比折扣值 5 9重载方法 第一个leaseSlip方法具有一个空的参数表 第二个leaseSlip方法具有折扣的形参变量 publicdoubleleaseSlip doubleaDiscountPercent doublefee this leaseSlip doublediscountedFee fee 100 aDiscountPercent 100 returndiscountedFee 5 9重载方法 publicclassTesterFive publicstaticvoidmain Stringargs create2SlipinstancesusingdifferentconstructorsSlipfirstSlip newSlip 1 10 20 SlipsecondSlip newSlip 2 retrieve 5 9重载方法 Slip Id 1 Width 10 Length 20 0Slip Id 2 Width 12 Length 25 0Slip1feeis800 0with10 discountis720 0Slip2feeis900 0with20 discountis720 0Pressanykeytocontinue 5 9重载方法 Java使用异常来通知在系统运行时可能出现的错误 问题和其他异常情况在Java中 异常也是对象 5 10处理异常 Java使用5个关键字来处理异常 try catch finally throw throws 客户端使用try catch finally 服务器端使用 throw throws 5 10处理异常 客户端每次调用可能会创建和抛出异常的方法时 都要将其调用的代码放置于try代码块中 服务器方法会通过关键字throws包含在其头内来抛出异常客户端会在catch代码块中捕获异常并处理final代码块可选 一旦包含在内 不管是否捕获异常都会执行 5 10处理异常 1 slipId的数据验证 设Bradshaw码头所连接的船台永远都不会多于50个 需要在setSlipIdsettermethod中验证传递给它的Id值是否在1 50范围内 如果某方法准备创建和抛出异常 其头必须包含关键字throws 5 10处理异常 扩展后的setSlipId为 publicvoidsetSlipId intanId throwsException在此方法中检查接收到的参数值是否在合理的范围内 如果超出了范围 则对Exception进行实例化 并将其抛出 注意不要在if语句中编写数字50 可以向Slip类中添加常量MAXIMUM NUMBER OF SLIPS 5 10处理异常 publicvoidsetSlipId intanId throwsException rejectslipIdifmaximumif anIdMAXIMUM NUMBER OF SLIPS Exceptione newException SlipIDnotbetween1 5 10处理异常 2 width的数据验证 setWidth方法中的width参数值为 10 12 14 16 首先向Slip类中添加数组来存储有效的宽度值 privatestaticfinalintVALID SLIP WIDTHS 10 12 14 16 下面向setWidth头中添加throwsException publicvoidsetWidth intaWidth throwsException 5 10处理异常 publicvoidsetWidth intaWidth throwsException booleanvalidWidth false for inti 0 i VALID SLIP WIDTHS length 5 10处理异常 带有数据验证的Slip类定义 在此示例中 构造函数方法实际调用的是setter方法 由于setter方法可能会抛出异常 因此构造方法头必须包括throwsException 5 10处理异常 publicclassSlip attributesprivateintslipId privateintwidth privatedoubleslipLength staticattributevariableprivatestaticintnumberOfSlips 0 staticconstantsfordefaultattributevaluesprivatestaticfinalintDEFAULT WIDTH 12 privatestaticfinalintDEFAULT SLIP LENGTH 25 staticconstantsfordatavalidationprivatestaticfinalintMAXIMUM NUMBER OF SLIPS 50 privatestaticfinalintVALID SLIP WIDTHS 10 12 14 16 5 10处理异常 constructorwith3parameterspublicSlip intanId intaWidth doubleaSlipLength throwsException invokesetterstopopulateattributessetSlipId anId setWidth aWidth setSlipLength aSlipLength numberOfSlips constructorwith1parameterpublicSlip intanId throwsException invoke3 parameterconstructorwithdefaultwidth 5 10处理异常 publicdoubleleaseSlip doublefee switch width case10 fee 800 break case12 fee 900 break case14 fee 1100 break case16 fee 1500 break default fee 0 returnfee overrideleaseSlippublicdoubleleaseSlip doubleaDiscountPercent doub

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论