6习题解析 6 -第6章 子类和继承_第1页
6习题解析 6 -第6章 子类和继承_第2页
6习题解析 6 -第6章 子类和继承_第3页
6习题解析 6 -第6章 子类和继承_第4页
6习题解析 6 -第6章 子类和继承_第5页
已阅读5页,还剩6页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

第6章子类和继承习题解析1.选择题(1)父类中的方法被以下哪个关键字修饰后不能被重写?()A、publicB、staticC、finalD、void答案:C(2)如果想在子类Bird中使用父类的带参数的构造方法,则需要在子类Bird的构造方法中通过代码调用,正确的是()。A.publicBird(){super("羽毛");}B.super();C.publicStringskin="羽毛";D.Birdbird=newBird();答案:A(3)现有两个类A、B,以下描述中表示B继承自A的是?()A、classAextendsB.classB、classBimplementsAC、classAimplementsBD、classBextendsA答案:D(4)下面代码是方法重载的是()。A.publicfloatgetArea(floatr){floatarea=PI*r*r;returnarea;}publicfloatgetArea(floatl,floatw){ //重载getArea()方法floatarea=l*w;returnarea;}B.publicfloatgetArea(floatr){ floatarea=PI*r*r;returnarea;}publicfloatgetArea(floatl{ //重载getArea()方法floatarea=lreturnarea;}C.publicfloatgetArea(floatr){ floatarea=PI*r*r;returnarea;}D.publicfloatgetTrea(floatr){ floatarea=PI*r*r;returnarea;}publicfloatgetArea(floatl,floatw){ //重载getArea()方法floatarea=l*w;returnarea;}答案:A((5)Java语言的类间的继承关系是()。A、多重的B、单重的C、线程的D、不能继承答案:B(6)下列方法中,哪个是抽象方法()。A.abstractvoidFunc(){}B.virtualvoidFunc(){}C.abstractvoidFunc();D.overridevoidFunc()答案:C(7)下列关于对象的类型转换的描述,说法错误的是()。A、对象的类型转换可通过自动转换或强制转换进行B、无继承关系的两个类的对象之间试图转换会出现编译错误C、由new语句创建的父类对象可以强制转换为子类的对象D、子类的对象转换为父类类型后,父类对象不能调用子类的特有方法答案:C(8)在下面哪种情况下,可以使用方法重写?()A、父类方法中的形参不适用于子类使用时B、父类中的方法在子类中没有时C、父类的功能无法满足子类的需求时D、父类方法中的返回值类型不适合子类使用答案:C(9)下面哪个修饰符不可以修饰接口中的成员变量?()A、publicB、staticC、finalD、private答案:D(10)编译运行下面的程序,结果是什么?()publicclassA{ publicstaticvoidmain(String[]args){ Bb=newB(); b.test(); } voidtest(){ System.out.print("A"); }}classBextendsA{ voidtest(){ super.test(); System.out.print("B"); }}A、产生编译错误B、代码可以编译运行,并输出结果ABC、代码可以编译运行,但没有输出D、编译没有错误,但会产生运行时异常答案:B2.填空题(1)如果子类想使用父类中的成员,可以通过关键字(super)引用父类的成员。(2)在Java语言中,用(abstract)修饰符定义的类为抽象类。(3)Java中一个类最多可以继承(一)个类。(4)在定义方法时不写方法体,这种不包含方法体的方法为(抽象)方法。(5)final修饰的局部变量只能被赋值(一)次。(6)在Java中一个接口可以继承多个接口,继承的接口之间使用(逗号)隔开即可。(7)一个类如果要实现一个接口,可以通过关键字(implements)来实现这个接口。3.综合题(1)类继承的实现方法,使用Eclipse创建名为“task5_1”的Java项目,在该项目中创建一个名为“Person”的Java类,该类实现左下图所示Person类UML类图所示的功能。在“task5_1”项目中再创建一个名为“Student”的Java类,Student类继承于Person类,同时实现左下图所示Student类UML类图所示的功能。其中,Person类的introduce方法和Student类tellStudentNo方法的输出效果如中下图所示。参考答案:packagetask5_1;publicclassPerson{Stringname;doubleheight;doubleweight;voidIntroduce(){ System.out.println("Mynameis"+""+name); System.out.println("Myheightis"+""+height); System.out.println("Myweightis"+""+weight);}}classStudentextendsPerson{StringstudentNo;voidtellStudentNo(){ System.out.println("MystudentNois"+""+studentNo);}intadd(intx,inty){ returnx+y;}intsub(intx,inty){ returnx-y;}}packagetask5_1;importtask5_1.Person;;publicclasstest{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Students=newStudent(); ="yly"; s.height=173.0; s.weight=63.0; s.studentNo="971115"; s.Introduce(); s.tellStudentNo(); }}(2)验证对象的上转型对象的功能特点,使用Eclipse创建Java项目“task5_6”,在该项目中创建一个名为“Task5_6”的Java类。Task5_6.java文件中的代码如下图所示(其中包含了Person类和Student类的代码)。回答上图代码中所示的6个问题,答案直接写在网络教学平台的答题区。参考答案:packagetask5_6;classPerson{ privateStringcardID; Stringname="无名氏"; publicPerson(Stringid){ cardID=id; } StringgetCardID(){ returncardID; } voidintroduceSelf(){ System.out.println("MycardIDis"+cardID); System.out.println("Mynameis"+name); System.out.println("ProduceSelf()"); }}classStudentextendsPerson{ privateStringstudentID; StringSchoolName; publicStudent(StringcID,StringsID){ super(cID); studentID=sID; } voidintroduceSelf(){ roduceSelf(); System.out.println("MystudentIDis"+studentID); System.out.println("SroduceSelf()"); } StringgerStudentID(){ returnstudentID; }}publicclassTask5_6{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Personp=newStudent("33062378","971115"); Students=newStudent("33062378","971115"); ="yly"; System.out.println(s.getCardID()); System.out.println(s.gerStudentID()); roduceSelf(); }}(3)通过实验理解什么是多态使用Eclipse创建Java项目“task5_7”,在该项目中创建一个名为“Task5_7”的Java类。Task5_7.java文件中的代码如下图所示(其中包含了Human类、Chinese类和American类的代码)。写出项目Task5_7运行后输出的内容,答案直接写在网络教学平台的答题区。参考答案:packagetask5_7;classHuman{ doubleheight; doubleweight; voidintroduce(){ System.out.println("__#*(*--&%...#"); }}classChineseextendsHuman{ voidintroduce(){ System.out.println("我是中国人"); System.out.println("我的身高是"+height+"厘米"); System.out.println("我的身体是"+weight+"公斤"); }}classAmericanextendsHuman{ voidintroduce(){ System.out.println("IamAmerican"); System.out.println("Myheightis"+height+"cm"); System.out.println("Myweightis"+we

温馨提示

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

评论

0/150

提交评论