(一)学习总结1.在上周完成的思维导图基础上,补充本周的学习内容,对Java面向对象编程的知识点做一个全面的总结。参考资料:XMind
2.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路并画出类图。工具:PowerDesigner参考教程:UML简介1,创建接口busisness得到汽车的编号,名称和租金,然后构造设置属性。2,分别设置类keche,huoche,pika,得到zaikeliang,zaihuolang,zaikeliang和zaihuolaing,并设置构造属性。3,测试类test,进行设置实例化并调用输出操作。
3.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果
interfaceAnimal{voidbreathe();voidrun();voideat();}classDogimplementsAnimal{publicvoidbreathe(){System.out.println("I'mbreathing");}voideat(){System.out.println("I'meating");}}publicclassTest{publicstaticvoidmain(String[]args){Dogdog=newDog();dog.breathe();dog.eat();}}改正后的程序:
interfaceAnimal{voidbreathe();voidrun();voideat();}classDogimplementsAnimal{publicvoidbreathe(){System.out.println("I'mbreathing");} publicvoidrun(){ System.out.println("I'mrunning"); } publicvoideat(){System.out.println("I'meating");} }publicclassTest{publicstaticvoidmain(String[]args){Dogdog=newDog();dog.breathe();dog.run();dog.eat();}}运行结果:
I'mbreathingI'mrunningI'meating修改方案:接口Animal中定义了三个抽象方法:
voidbreathe();voidrun();voideat();所以在定义接口实现类中要覆写这三个的抽象方法:publicvoidbreathe(){},publicvoidrun(){},publicvoideat(){}在test类中也要对这三个类对象进行实例化并调用:
Dogdog=newDog();dog.breathe();dog.run();dog.eat();4.运行下面的程序
importjava.util.Arrays;publicclassTest{publicstaticvoidmain(String[]args){String[]fruits={"peach","banana","orange","apple"};Arrays.sort(fruits);for(inti=0;i 程序修改后: importjava.util.Arrays;publicclassTest{publicstaticvoidmain(String[]args){String[]fruits={"peach","banana","orange","apple"};Arrays.sort(fruits);System.out.println("正序:\n");for(inti=0;i 正序:applebananaorangepeach倒序:peachorangebananaapple5.其他需要总结的内容。(二)实验总结实验内容:1.某工厂生产各种音乐盒,客户无需知道音乐盒的制作过程,只需知道如何播放音乐盒即可。用简单工厂设计模式实现该过程:接口MusicBox具有方法play(),两个音乐盒类PianoBox,ViolinBox,MusicBoxFactory产生MusicBox的实例。1,在设置加工类中使用: if("piano".equals(className)){ m=newPianoBox(); } if("violin".equals(className)){ m=newViolinBox(); }进行曲目的名称输入。2,在定义子类ViolinBox和PianoBox中分别覆写play()方法ViolinBox中:publicvoidplay(){System.out.println("演奏小提琴曲.");}3,测试类中:Scanner方法进行输入字符串:piano和violin进行选择曲目并调用: Employee[]e=newEmployee[3]; try{ e[0]=newEmployee("c酱","女",newSimpleDateFormat("yyyy-mm-dd").parse("1999-3-15"),newSimpleDateFormat("yyyy-mm-dd").parse("2017-1-25")); e[1]=newEmployee("l酱","男",newSimpleDateFormat("yyyy-mm-dd").parse("1987-8-15"),newSimpleDateFormat("yyyy-mm-dd").parse("2016-11-15")); e[2]=newEmployee("k酱","男",newSimpleDateFormat("yyyy-mm-dd").parse("1977-5-10"),newSimpleDateFormat("yyyy-mm-dd").parse("2015-9-16")); } catch(ParseExceptionc){ c.printStackTrace(); }3,使用 没做完,已经实现选择购买单个宠物的功能,不能实现多个宠物的购买的功能(及计算总价格)。