《Java语言程序设计》答案.pdf





《《Java语言程序设计》答案.pdf》由会员分享,可在线阅读,更多相关《《Java语言程序设计》答案.pdf(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、.中国石油大学()远程教育学院期 末 考 试 JavaJava 语言程序设计语言程序设计 学习中心:学习中心:_ _ _:_学号:学号:_关于课程考试违规作弊的说明关于课程考试违规作弊的说明1、提交文件中涉嫌抄袭内容(包括抄袭网上、书籍、报刊杂志及其他已有论文),带有明显外校标记,不符合学院要求或学生本人情况,或存在查明出处的内容或其他可疑字样者,判为抄袭,成绩为“0”。2、两人或两人以上答题内容或用语有 50%以上相同者判为雷同,成绩为“0”。3、所提交试卷或材料没有对老师题目进行作答或提交内容与该课程要求完全不一、题型一、题型简答题,4 题,每题 15 分,共 60 分;编程题,2 题,每
2、题 20 分,共 40 分二、题目二、题目共两套题,学号末尾数单号学生做第一套,学号末尾数双号学生做第二套题共两套题,学号末尾数单号学生做第一套,学号末尾数双号学生做第二套题第一套题第一套题一、简答题1 什么是多态性?方法的重载和覆盖有何区别?阅读下列代码,指出其中存在的重载和覆盖,写出输出结果是什么?解释为什么这样输出?(15 分)classclass C1classclass C2 extendsextends C1classclass C3.publicpublic voidvoid f()System.out.println(C1.f);publicpublic voidvoid f(
3、)System.out.println(C2.f);publicpublic voidvoid g(C1 one)System.out.println(g(C1);one.f();.publicpublic voidvoid g(C2 two)System.out.println(g(C2);two.f();publicpublic classclass Main1publicpublic staticstatic voidvoid main(String args)C1 one=newnew C2();C3 three=newnew C3();three.g(one);答:1.多态性是指允许
4、不同类的对象对统一消息做出响应。多态包括参数化多态和包含多态。多态性语言具有灵活、抽象、行为共享、代码共享等优势,很好地解决了应用程序函数同名问题。重载是指同一个类中有多个同名的方法,但这些方法有着不同的参数,因此在编译时就可以确定到底调用哪个方法,它是一种编译时多态。覆盖是指子类可以覆盖父类的方法,同样的方法会在子类和父类中有着不同的变现形式。即重载是一个类的多态性表现,而覆盖是子类和父类的一种多态性表现。classclass C1publicvoidpublicvoid f()System.outout.println(C1.f);.classclass C2 extendsextends
5、 C1publicvoidpublicvoid f()System.outout.println(C2.f);/覆盖,子类 C2 重写了父类 C1 中的 f()方法classclass C3 publicvoidpublicvoid g(C1 one)System.outout.println(g(C1);one.f();publicvoidpublicvoid g(C2 two)/重载,根据传入参数不同,C3 实现不同的 g()方法System.outout.println(g(C2);two.f();publicclasspublicclass Main1publicstaticvoidp
6、ublicstaticvoid main(String args)C1 one=newnew C2();C3 three=newnew C3();three.g(one);.输出:g(C1)C2.f原因:由于子类 C2 的 f()方法与父类 C1 的方法同名,因此 C2 的方法会覆盖 C1的方法。在执行 C1 one=new C2()语句时,会调用 C1 类的构造函数,因此three.g(one)语句会调用传入参数为 C1 的 g()方法,输出 g(C1)。而由于实际创建的是 C2 类的方法,后面的方法调用都会调用子类 C2 的方法,因此输出 C2.f。2、请用自己的语言介绍 throw/th
7、rows 有什么联系和区别?在程序中应如何使用?另外谈谈final、finally 的区别和作用,必须举例说明用法。(15 分)答:若某个方法可能会发生异常,但不想在当前方法中处理这个异常,则可以使用 throws、throw 关键字在方法中抛出异常。throw 关键字通常用于方法体中,并且抛出一个异常对象,程序在执行到 throw 语句时立即终止,它后面的语句都不执行。throws 关键字通常被应用在声明方法时,用来指定方法可能抛出的异常,多个异常可以使用逗号分隔,使用 throws 关键字将异常抛给上一级后,如果不想处理该异常,可以继续向上抛出,但最终要有能够处理该异常的代码。/*在类 M
8、ain 中创建 pop()方法,在该方法中抛出 Exception 异常,在主方法中调用该方法,并实现异常处理*/publicclasspublicclass Main/创建类staticvoidstaticvoid pop()throwsthrows Exception./定义方法并抛出异常intinta=1/0;/除数为 0publicstaticvoidpublicstaticvoid main(String args)/主方法trytry pop();/调用 pop()方法 catchcatch(Exception e)System.outout.println(pop()方法抛出的异
9、常);/输出异常信息/*使用 throw 关键字直接在程序中抛出异常*/publicclasspublicclass Main publicstaticvoidpublicstaticvoid main(String args)intinta=1;if if(a=1)thrownewthrownew NumberFormatException();elseelse.System.outout.println(a);final 用于声明属性、方法和类,分别表示属性不可变、方法不可覆盖和类不可被继承。Finally 作为异常处理的一部分,只能用在 try/catch 语句中,并且附带一个语句块,表
10、示这段语句最终一定被执行。/*类 Main 被声明为 final,所以不能被继承,所有方法都不能被重写。*finally 中的代码一定会被执行,即使程序被 return 中。*/publicfinalclasspublicfinalclass Main publicstaticvoidpublicstaticvoid main(String args)Main m=newnew Main();trytry thrownewthrownew NullPointerException();catchcatch(NullPointerException e)System.outout.println
11、(程序抛出了异常);finallyfinally.System.outout.println(执行了 finally 语句块);3、编写一个描述学生基本情况的类,属性包括姓名,学号,语文成绩,数学成绩和英语成绩,方法包括信息输出,设置姓名和学号,设置三门课程成绩,计算总成绩和平均成绩。在main 方法中对方法进行测试(15 分)答:publicclasspublicclass Mainprivateprivate String number,name;privatedoubleprivatedoubleChinese,Math,English;publicpublic Main(String
12、a,String b,doubledoublec,doubledoubled,doubledoublee)/有参构造函数number=a;name=b;Chinese=c;Math=d;English=e;publicvoidpublicvoid setName(String name)/设置姓名thisthis.name=name;publicvoidpublicvoid setNumber(String num)/设置学号.thisthis.number=num;publicvoidpublicvoid setChinese(doubledoublescore)/设置语文成绩thisthi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言程序设计 Java 语言程序设计 答案

限制150内