java接口与实现接口的类实验报告.pdf





《java接口与实现接口的类实验报告.pdf》由会员分享,可在线阅读,更多相关《java接口与实现接口的类实验报告.pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验报告实验报告课程名称实验名称学生学号面向对象程序设计实验六、接口与实现接口的类姓名日期班级2018/4/29实验目的:通过本次实验,要求学生能进一步掌握接口、包中的基础类库和包中的工具类库的使用方法。实验条件:电脑一台、能上网查阅资料。1.定义 Person 类,包含 name(姓名)、birthday(出生日期)、idCard(身份证号)、sex(性别)、age(年龄)等基本信息,通过使用Comparator 接口实验Person 类可以按姓名、年龄、出生日期等多种比较规则进行排序。源代码:源代码:packagepackage person;publicpublic classclass
2、 Person String name;String idCard;String birthday;charchar sex;intint age;publicpublic Person()publicpublic Person(String name,String idCard,String birthday,charchar sex,intint age)supersuper();=name;=idCard;=birthday;=sex;=age;packagepackage person;importimportclassclass CompareName implementsimple
3、ments ComparatorOverridepublicpublic intint compare(Person p1,Person p2)ame+性别:+personi.sex+年龄:+personi.age+生日:+personi.birthday);n按姓名排序后顺序为:);(person,newnew CompareName();forfor(intint i=0;in;i+)n按年龄排序后顺序为:);(person,newnew CompareAge();forfor(intint i=0;in;i+)n按生日排序后顺序为:);(person,newnew CompareBirt
4、hday();forfor(intint i=0;in;i+)姓名:+personi.name+性别:+personi.sex+年龄:+personi.age+生姓名:+personi.name+性别:+personi.sex+年龄:+personi.age+生姓名:+personi.name+性别:+personi.sex+年龄:+personi.age+生日:+personi.birthday);日:+personi.birthday);日:+personi.birthday);实验结果:实验结果:2.首先定义基本几何图形接口(IShape),然后定义 IShape 的实现类 Circle
5、(圆形)、Square(正方形)和 Triangle(三角形)。要求计算各种图形的面积和周长。(提示:求三角形的面积用海伦公式:S L*(La)*(Lb)*(Lc),其中 a,b,c是三角形的边长,L=(a+b+c)/2,即周长的一半。)源代码:源代码:packagepackage jiekoushape;publicpublic interfaceinterface IShape publicpublic doubledouble area();publicpublic doubledouble length();packagepackage jiekoushape;publicpublic
6、 classclass Circle implementsimplements IShape privateprivate String name;privateprivate doubledouble r;publicpublic Circle(String name,doubledouble r)supersuper();=name;=r;publicpublic String getName()returnreturn name;publicpublic voidvoid setName(String name)=name;publicpublic doubledouble getR()
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 接口 实现 实验 报告

限制150内