1、18. 下列选项中,定义接口MyInterface的语句正确的是:(A)A)interface MyInterface B) implements MyInterface C) class MyInterface D) implements interface My 19. 如果子类中的方法mymethod()覆盖了父类中的方法mymethod(),假设父类方法头部定义如下:void mymethod(int a),则子类方法的定义不合法的是:(C)A) public void mymethod(int a) B) protected void mymethod(int a)C) privat
2、e void mymethod(int a) D) void mymethod(int a)二、 填空题1. 如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量_了父类的同名变量。(隐藏)2. 属性的隐藏是指子类重新定义从父类继承来的_。(同名变量或属性)3. 如果子类中的某个方法的名字、返回值类型和_与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。(参数列表) 4. Java 仅支持类间的_重继承。(单重)5. 抽象方法只有方法头,没有_。 (方法体)6. Java 语言的接口是特殊的类,其中包含_常量和_方法。(static(静态)
3、; abstract(抽象)7. 接口中所有属性均为_、_和_的。 (public、static、final)8. 如果接口中定义了一个方法methodA(),一个属性attA,那么一个类ClassA要实现这个接口的话,就必须实现其中的_方法。(methodA()9.一个类如果实现一个接口,那么它就必须实现接口中定义的所有方法,否则该类就必须定义成_的。(抽象的) 10. 如果子类中的某个方法的名字、_和参数列表与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。(返回值类型) 11. Java 仅支持类间的单重继承,接口可以弥补这个缺陷,支持_重继承(多重)12. 在方
4、法头用abstract修饰符进行修饰的方法叫做_方法。(抽象)13.Java语言中用于表示类间继承的关键字是_。(extends)14. 接口中所有方法均为_和_的。 (public、abstract)15. Java语言中,表示一个类不能再被继承的关键字是_。(final)16. Java语言中,表示一个类A继承自父类B,并实现接口C的语句是_。 (class A extends B implements C)。17. 如果子类中的方法compute()覆盖了父类中的方法compute(),假设父类的compute()方法头部有可见性修饰符public,则methodS()的可见性修饰符必须
5、是_。(public)三、 程序阅读题1.现有类说明如下,请回答问题:public class A String str1= Hello! t; String str2= How are you? public String toString( ) return str1+str2; public class B extends Abb,Bill. return super.str1+str1;问题:1)类A和类B是什么关系?(继承关系)2)类A和类B都定义了str1属性和方法toString( ), 这种现象分别称为什么?(属性的隐藏;方法的覆盖)3)若a是类A的对象,则a.toString
6、( )的返回值是什么? (Hello! How are you?)4)若b是类B的对象,则b.toString( )的返回值是什么?(Hello,Bill.)2.现有一个类定义如下,请回答问题: class Employee String name; int age; double wage; static int No=0; Employee(String a1,int a2,double a3) name=a1; age=a2; wage=a3; No+; 在使用该类时,已使用下面语句生成了该类的对象: Employee e1,e2; e1=new Employee(王劲,26,6300)
7、; e2=new Employee(张山,30,3800);1)e2.name,e2.age,e2.wage的值各是什么?(张山;30;3800.0)2)生成对象e1、e2后,e1.No值为多少?能否通过类名做前缀引用属性No?(2; 能)3.阅读程序,回答问题。public class InheritTest1 public static void main (String args) A aa; B bb; aa=new A( ); bb=new B( ); aa.show( ); bb.show();class A int a=1; double d=2.0; void show( )
8、System.out.println(Class A:+ta=+a +td=+d);class B extends A float a=3.0f; String d=Java program. int b=4; void show( ) System.out.println(+super.a +super.d); super.show( );Class B:+d+tb=+b);1)这是哪一类java程序?(java应用程序) 2)类A和类B是什么关系?(类B是类A的子类)3)按程序输出的格式写出程序运行后的结果.(程序运行结果如下: a=1 d=2.0 a=3.0 d=Java program
9、. b=4 )4.现有类说明如下,请回答问题: int x=10; int getA()return x; int x=100; int getB()return x;1)类B是否能继承类A的属性x?(能) 2)若b是类B的对象,则b.getB()的返回值是什么?(100) 3)若b是类B的对象,则b.getA()的返回值是什么?(10)4)类A和类B都定义了x属性,这种现象称为什么?(属性的隐藏)5.有如下源程序,请回答问题: class A String s=class A class B extends Aclass B public class TypeConvert public s
10、tatic void main(String args) B b1,b2=new B(); A a1,a2; a1=(A)b2; a2=b2; System.out.println(a1.s); System.out.println(a2.s); b1=(B)a1; System.out.println(b1.s); System.out.println(b2.s); 问题: 该程序的四行输出各是什么?(class Aclass Bclass B)6.现有类说明如下,请回答问题: int x=888; String str=I like: public String toString() re
11、turn str+x; String x=java return str+x+ and +super.x; 2)类A和类B都定义了x属性和方法toString(),这种现象分别称为什么?(属性的隐藏和方法的覆盖) 3)若a是类A的对象,则a.toString( )的返回值是什么?(I like: 888) (I like: java and 888)7.运行类C的输出结果是什么? public A() System.out.println(“The default constructor of A is invoked”); public B()public class C public st
12、atic void main(String args) B b = new B();8.阅读下列程序写出输出结果: void show() System.out.println(s); B b1;B b2=new B(); a1.show(); a2.show(); b1.show(); b2.show();答案:四、 程序填空题1.下面是一个类的定义,完成程序填空。(Youwrite this.x=x;public class Youwrite int x;_( ) x=0; Youwrite ( int x) _;2.下面是定义一个接口ITF的程序,完成程序填空。 (interface a
13、bstract )public _ ITF public static final double PI=Math.PI; public _ double area(double a, double b);3.下面是定义一个接口A的程序,完成程序填空。(final ”;” )public interface A public static _ double PI=3.14159; public abstract double area(double a, double b)_五、 编程题简单类的继承:1.定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。编写一个类,继承自矩形类,同
14、时该类描述长方体,具有长、宽、高属性,和计算体积的方法。编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。public class TestExtends public static void main(String args) LiFangTi f=new LiFangTi(); f.length=2; f.width=3; f.height=7; System.out.println(立方体的底面积为:+f.findArea()+立方体的体积为:+f.findVolume();class JuXing int length; int width;
15、int findArea() return length*width;class LiFangTi extends JuXing int height; int findVolume() return findArea()*height;3. 定义一个Person类,它包括的属性有“姓名”和“性别”,为Person类派生出一个子类Student类,为Student子类添加两个属性年龄和成绩等级(用A,B,C,D,E表示),在子类中打印出学生的姓名、性别、年龄及成绩等级。class Person protected String name; protected String sex; Perso
16、n(String name,String sex) this.name=name; this.sex=sex; public String toString() String s=new String(); s=name+t+sex; return s;class Student extends Person protected int age; protected char grade; Student(String name,String sex,int age,char grade) super(name,sex); this.age=age; this.grade=grade; voi
17、d print() s=super.toString(); s=s+age+grade; System.out.println(s);public class TestP Student st1=new Student(zhangfei,male,23,E); Student st2=new Student(liubei,25,Anametsettagetgrade st1.print(); st2.print();4.定义一个类Rectangle代表矩形,其中包括计算面积的方法。再定义一个它的子类Square代表正方形,其中也包含计算面积的方法。编写一程序,测试新建子类对象的属性和方法。cl
18、ass Rectangle float length; float width; Rectangle(float len,float wh) length=len; width=wh; float getArea()class Square extends Rectangle Square(float len) super(len,len); return super.getArea();public class TestRectangle Square sq=new Square(5.2f);side is +sq.length+,area is +sq.getArea();类与对象习题一、
19、选择题1 定义类头时,不可能用到的关键字是( B)。 A) class B)private C)extends D)public2.下列类定义中,不正确的是(C)。A) class x . B) class x extends y . C) static class x implements y1,y2 . D) public class x extends Applet . 3.下列类头定义中,错误的是( A)。 A)public x extends y . B) public class x extends y . C)class x extends y implements y1 . D
20、)class x .4.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是( D)。 A) float A a; B) public A a=A( ); C) A a=new int( ); D) static A a=new A( );5.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是(A)。 A) public A a=new A( ); C) A a=new class( ); D) a A;6.设 X 、Y 均为已定义的类名,下列声明类X的对象x1的语句中正确的是(C)。 A) public X x1= new Y( ); B) X x1= X ( ); C) X x1=new X( ); D) int X x1;7. 设X 、Y为已定义的类名,下列声明X类的对象x1的语句中正确的是(A)。 A) static X x1; B) public X x1=new X(int 123); C) Y x1; D) X x1= X( );8.通过类MyCla
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1