编写问题域类定义.ppt
《编写问题域类定义.ppt》由会员分享,可在线阅读,更多相关《编写问题域类定义.ppt(87页珍藏版)》请在三一办公上搜索。
1、第5章 编写问题域类定义,课程回顾UML三层设计,第5章 编写问题域类定义,开发PD类定义测试PD类编写构造函数方法编写toString方法为Slip类编写定义编写自定义方法格式化输出使用静态变量和静态方法重载方法处理异常,第5章 编写问题域类定义,编写规范类名:大写字母开头,Customer属性名:小写字母开头,但后面的字可用大写字母,phoneNo方法名:动词在前,名次在后,小写字母开头,setAddress,第5章 编写问题域类定义,首先以客户类为例子定义Customer类定义属性:private String name;private String address;private St
2、ring phoneNo;定义方法:get方法,set方法。,5.1 开发PD类定义,5.1 开发PD类定义,Java类的结构定义,public class Customer/attribute definitionsprivate String name;private String address;private String phoneNo;/get方法public String getName()return name;public String getAddress()return address;public String getPhoneNo()return phoneNo;,5
3、.1 开发PD类定义,/set方法public void setName(String newName)name=newName;public void setAddress(String newAddress)address=newAddress;public void setPhoneNo(String newPhoneNo)phoneNo=newPhoneNo;,5.1 开发PD类定义,5.1 开发PD类定义,public class TesterOnepublic static void main(String args)Customer firstCustomer=new Custo
4、mer();/create instance/invoke set accessors to populate attributesfirstCustomer.setName(Eleanor);firstCustomer.setAddress(Atlanta);firstCustomer.setPhoneNo(123-4567);/define variables to contain attribute values retrievedString customerName,customerAddress,customerPhoneNo;,5.2 测试PD类,/invoke get acce
5、ssors to retrieve attribute values customerName=firstCustomer.getName();customerAddress=firstCustomer.getAddress();customerPhoneNo=firstCustomer.getPhoneNo();/display the retrieved attribute valuesSystem.out.println(The name is+customerName);System.out.println(The address is+customerAddress);System.
6、out.println(The phone is+customerPhoneNo);,5.2 测试PD类,TesterOne序列图,5.2 测试PD类,创建多个实例?,5.2 测试PD类,构造方法:默认的构造方法,自定义构造方法。默认的构造方法仅由头和一个空的代码块组成。Public Customer(),5.3 编写构造方法,自定义构造函数(参数化的构造函数):public Customer(String aName,String anAddress,String aPhoneNo)/invoke setters to populate attributesthis.name=aName;a
7、ddress=anAddress;phoneNo=aPhoneNo;,5.3 编写构造方法,创建多个实例:姓名:Eleanor Mike JoAnn地址:Atlanta Boston St.Louis电话号码:123-4567 456-1234 765-4321,5.3 编写构造方法,编写TesterThree类对Customer类中的构造函数方法进行测试:public class TesterThree public static void main(String args)/define the reference variables Customer firstCustomer,seco
8、ndCustomer,thirdCustomer;/create three Customer instances with attribute values firstCustomer=new Customer(Eleanor,Atlanta,123-4567);secondCustomer=new Customer(Mike,Boston,467-1234);thirdCustomer=new Customer(JoAnn,St.Louis,765-4321);/retrieve,5.3 编写构造方法,作用:检索实例所有的属性值,将它们置于一个String对象中。public String
9、 toString()String info;info=Customer name=+getName()+,Address=+getAddress()+,Phone Number=+getPhoneNo();return info;,5.4 编写toString方法,public class TesterFourpublic static void main(String args)/define the reference variables Customer firstCustomer,secondCustomer,thirdCustomer;/create three Customer
10、instances with attribute values firstCustomer=new Customer(Eleanor,Atlanta,123-4567);secondCustomer=new Customer(Mike,Boston,467-1234);thirdCustomer=new Customer(JoAnn,St.Louis,765-4321);/invoke tellAbutSelf for all three customers,5.4 编写toString方法,SlipslipIdwidthslipLength,5.5 为Slip类编写定义,Slip类的类图,p
11、ublic class Slip/attributesprivate int slipId;private int width;private double slipLength;/constructor with 3 parameterspublic Slip(int anId,int aWidth,double aSlipLength)/invoke setters to populate attributessetSlipId(anId);setWidth(aWidth);setSlipLength(aSlipLength);,5.5 为Slip类编写定义,/set methods pu
12、blic void setSlipId(int anId)slipId=anId;public void setWidth(int aWidth)width=aWidth;public void setSlipLength(double aSlipLength)slipLength=aSlipLength;/get methodspublic int getSlipId()return slipId;public int getWidth()return width;public double getSlipLength()return slipLength;,5.5 为Slip类编写定义,/
13、custom method toStringpublic String toString()String info;info=Slip:Id=+getSlipId()+,Width=+getWidth()+,Length=+getSlipLength();return info;,5.5 为Slip类编写定义,编写tester类(TestOne)来测试代码的正确性:TestOne 类仅有一个单一方法main,在mian中定义具有三个元素的数组slips来保存Slip实例。Slip slips=new Slip3;/构造函数的三个参数分别表示slipId,width,slipLength。Sli
14、ps0=new Slip(1,10,20);Slips1=new Slip(2,12,25);Slips2=new Slip(3,14,30);,5.5 为Slip类编写定义,public class TesterOnepublic static void main(String args)/create an array to hold three Slip referencesSlip slips=new Slip3;/create 3 Slip instancesslips0=new Slip(1,10,20);slips1=new Slip(2,12,25);slips2=new Sl
15、ip(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.0Press any key to continue.,5.5 为Slip类编写定义,编写自定义方法leaseSlip,计算船台的出租费用。出租费用:船台宽度 年租金 10$800 12$900 14$1100 16$1500,5.6 编写自定义方法,/custom method to lease a Slippub
16、lic double leaseSlip()double fee;switch(width)case 10:fee=800;break;case 12:fee=900;break;case 14:fee=1100;break;case 16:fee=1500;break;default:fee=0;return fee;,5.6 编写自定义方法,public class TesterTwopublic static void main(String args)/create an array to hold three Slip referencesSlip slips=new Slip3;/
17、create 3 Slip instancesslips0=new Slip(1,10,20);slips1=new Slip(2,12,25);slips2=new Slip(3,14,30);/compute,5.6 编写自定义方法,Fee is 800.0 for slip 1Fee is 900.0 for slip 2Fee is 1100.0 for slip 3Press any key to continue.,5.6 编写自定义方法,使用NumberFormat和DecimalFormat(在java.text包中):NumberFormat类提供将数字数据作为货币(带有逗号
18、,货币符号和小数点)进行格式化的方法,以及不同国家格式化货币的方法。使用DecimalFormat类对带有逗号和小数点,但未带有美元符号的数字进行格式化。,5.7 格式化输出,Class NumberFormat|+-|+-在使用NumberFormat类之前,需要使用import语句:import java.text.*;,5.7 格式化输出,public abstract class NumberFormat extends Format静态方法:getInstancepublic static final NumberFormat getInstance(),5.7 格式化输出,使用ge
19、tInstance方法,格式化数据:NumberFormat general=NumberFormat.getInstance();myString=general.format(myNumber);格式化多个数据:NumberFormat nf=NumberFormat.getInstance();for(int i=0;i a.length;+i)System.out.println(nf.format(myNumberi)+;);,5.7 格式化输出,import java.text.*;public class TesterThree public static void main(S
20、tring args)/create a Slip instanceSlip aSlip=new Slip(3,14,30);/compute lease fee double fee=aSlip.leaseSlip();System.out.println(fee);/illustrate number formatNumberFormat nf=NumberFormat.getInstance();System.out.println(number:+nf.format(fee);,5.7 格式化输出,1100.0number:1,100Press any key to continue.
21、,5.7 格式化输出,getCurrencyInstance 方法:public static final NumberFormat getCurrencyInstance()/Returns a currency format for the current default locale.public static NumberFormat getCurrencyInstance(LocaleinLocale)/Returns a currency format for the specified locale.,5.7 格式化输出,import java.text.*;import jav
22、a.util.*;public class TesterThreepublic static void main(String args)/create a Slip instance Slip aSlip=new Slip(3,14,30);/compute lease fee double fee=aSlip.leaseSlip();/illustrate currency format NumberFormat currencyFormat=NumberFormat.getCurrencyInstance();System.out.println(Currency:+currencyFo
23、rmat.format(fee);NumberFormat ccFormat=NumberFormat.getCurrencyInstance(Locale.US);System.out.println(Currency:+ccFormat.format(fee);,5.7 格式化输出,Currency:¥1,100.00Currency:$1,100.00Press any key to continue.,5.7 格式化输出,java.text Class DecimalFormat|+-|+-|+-,5.7 格式化输出,public class DecimalFormat extends
24、 NumberFormat/DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.It also supports different kinds of numbers,including integers(123),fixed-point numbers(123.4),scientific notation(1.23E4),percentages(12%).,5.7 格式化输出,DecimalFormat d1Format=new DecimalFormat(#,#0.00);#要显
25、示的数字保留位置,会取消所有的前导零0强制显示某个字符,即使字符的值为零,5.7 格式化输出,import java.text.*;import java.util.*;public class TesterThreepublic static void main(String args)/create a Slip instance Slip aSlip=new Slip(3,14,30);double fee=aSlip.leaseSlip();DecimalFormat d1Format=new DecimalFormat(#,#0.00);System.out.println(Deci
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编写 问题 定义

链接地址:https://www.31ppt.com/p-6056761.html