1、习题四参考答案 一、判断题 1.对 2.对 3.错 4.错 5.对 6.错 8.错 9.对 10.对 11.错 12.对 13.错 14.对 15.对 16.错 17.对 18.对 19.错 20.错 【解释】 ² 第3题:本题考查抽象类的特性。抽象类中可包含构造方法、抽象方法和具体实现的方法以及常量和变量,但不能直接实例化,一般可对抽象类的子类实例化,实例化对象引用可以
2、是抽象类类型。 ² 第11题:本题考查子类中可以写的方法。子类可以继承父类的方法,也可以覆盖父类的方法,也可以新增加方法,新增加的方法与从父类继承的方法可构成重载。 ² 第13题:本题考查abstract的用法。abstract可修饰类和方法,但不能修饰属性,即不能修饰类的成员变量。 ² 第16题:本题考查final修饰的方法的特性。final修饰的方法不能被覆盖,但可以有重载的方法。 二、选择题 1.B 2.B 3、A 4.B 5.A 6.C 7.B 8.D
3、 9.B 10.B 11.A 12.D 13、D 14.A 15.D 16.D 17、C 18.D 19、AD 20、CD 21、C 22、C 23、C 24、B 25.B 26.D 28.A 【解释】 ² 第7题:本题考查对继承、覆盖和重载的理解,属于难点。本题4个选项中的方法名与父类定义的方法名都相同。选项A的方法有3个参数,选项C的方法有1个参数,都能与从父类继承的方法构
4、成重载,可以加入子类Child中;选项B和选项D方法中的参数类型和个数都与父类定义的方法相同,由于父类定义的方法用public修饰,要想覆盖父类定义的方法,子类的方法也必须用public修饰,因此,选项D可以加入子类Child中,但选项B不行。 ² 第8、10、13和14题:这几题主要考查子类中含有覆盖方法的调用情况。子类对象调用方法时,首先调用子类中同名和参数形式(参数个数、类型和顺序)一致的方法,否则就调用父类中定义的匹配的方法。 ² 第9题:本题考查对类实现接口的理解。在Java的接口中包括抽象方法和常量。类实现接口时,需实现接口中的所有方法,并可直接使用接口中定义的常量(等同于从接
5、口中继承了这些常量)。本题中的接口B定义了"int k=10;”编译时会自动加上final,表示k为常量。因此,本题选B。 ² 第11题:本题考查父类中有static修饰和没有static修饰的方法被子类覆盖的情形。当父类的方法被static修饰时,子类中覆盖的方法也必须用static修饰。反之,父类的方法没有static修饰,子类中覆盖的方法也不用static修饰。所以,本题选A。 ² 第12和16题:这2题考查子类继承父类时构造方法的定义。子类继承父类时,若父类定义了有参数的构造方法,而没有定义无参数的构造方法,那么在子类中的构造方法必须用super语句去调用父类定义的有参数的构
6、造方法,否则无法通过编译。因此,建议在定义父类时,如写了构造方法,最好写上一个无参数的构造方法。 ² 第18题:本题考查类的继承。选项A中的m仅在父类的方法参数中定义,无法给变量i赋值;选项B中的b是非静态成员变量,在静态的main方法中无法直接给i赋值;父类定义的a是私有的,无法被子类继承,所以选项C也不对。选项D中的方法change是父类定义的公共方法,可以被子类继承和调用。因此,本题选D。 ² 第23题:本题考查类的构造方法。程序中第2行为构造方法,第5行为方法名,Test有void修饰,它不是构造方法,可作为实例方法。程序编译能通过。运行时,第9行实例化Test对象,调用第2行的构
7、造方法,输出3;然后执行第9行调用第5行的方法,输出2;最后执行11行,输出1。因此,本题选C。 ² 第24题:本题考查子类实例化时的过程 三、程序阅读题 1. int 2.(1)abstract (2)getName (3)extends (4)String major (5)getMajor 3. 32 4. resultOne=0 resultTwo=48 resultThree=20 5. Pine Tree Oops 6. My Func 7. 输出结果为: 2 8. hi! I a
8、m Tom How do you do? 9. 第2行改为final double PI=3.14; 第5行的extends改为implements 第10行的方法声明增加一个public, 改为public double area() 10. 15 11 10 11. 2 2 1 四、 编程题 1.参考代码如下: public class Circle { double radius; public Circle(){ radius=0;
9、 } public Circle(double r){ radius=r; } public double getRadius(){ return radius; } public double getPerimeter() ( return 2*Math.PI*radius; } public double getArea () { return Math.PI*radius*radius; } public void disp(){ System.o
10、ut.println ("圆的半径为"+getRadius()+"周长为"+getPerimeter()+"面积为"+getArea ()); } } public class Cylinder extends Circle{ double height; public Cylinder(double r,double h){ radius=r; height=h; } public double getHeight(){ return hei
11、ght; } public double getCylinderArea(){ return 2*Math.PI*radius*(radius+height); } public double getVol(){ return Math.PI*radius*radius*height; } public void disVol(){ System.out.println ("圆柱体的体积为"+getVol());
12、 } } 2.参考代码如下: interface Shape{ public abstract double getArea (); } class Circle implements Shape{ private int r; Circle (int r) {
13、 this.r=r; } public double getArea() { return 3.14*r*r; } } class TestCircle { public static void main (String args [] ) { Circle c=new Circle (5); System. out.println ("园的面积为:"+c.getArea()); } } 3.参考代码如下: interface sh
14、ape{ double area(); ) class Triangle implements shape{ private double a, b, c; public Triangle(double a, double b, double c){ this.a=a; this.b=b; this.c=c; } public double area(){ double p= (a+b+c)/2;
15、 return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } } class Circle implements shape{ private double r; public Circle(double r)( this.r=r; } public double area(){ return Math.PI*r*r; } } class Rectangle implements shape{ p
16、rivate double width, height; public Rectangle(double j, double k){ width=j; height=k; } public double area(){ return width*height; } } public class TestShape { public static void main(String args[]){ shape s[]=new shape[3]; s
17、[0]=new Triangle(3, 4,5); s[1]=new Circle(3.5); s[2]=new Rectangle(3.5,4.0); for (int i=0;i< s.length;i++ ) System.out.println (s[i].area()); } } 4.不需要做 5. (1) 参考代码如下: abstract public class Animal { public String name; public int age; public double weight;
18、 public void showInfo( ){ System. out. println ("动物名为"+name+"年龄为"+age+"岁,重量为"+weight ); } abstract public void move(); abstract public void eat(); } (2) 参考代码如下: public class Bird extends Animal{ public Bird(String name,int age,double weight){ th
19、is.name=name; this.age=age; this.weight=weight; } public void showInfo( ){ System. out.println ("鸟名为"+name+"年龄为"+age+"重量为"+weight); } public void move(){ System.out .println (name+ "用翅膀在天空上!"); } public void eat(){ System
20、out .println (name+ "喜欢吃虫子!"); } } (3) 参考代码如下: public class TestAnimal { public static void main (String args [] ) { Animal bird=new Bird ("麻雀",1,0.3); bird.showInfo(); bird.move(); bird.eat(); } 6.参考代码如下: public class People { public String name;
21、 public String sex; public String bothnum; public People(){}; public String printInfo(){ return"姓名:"+name+'\n'+"性别:"+sex+'\n'+"出生年月:"+bothnum; } } class Teacher extends People{ public String school; public int workID; public String printI
22、nfo(){ return super.printInfo()+'\n'+"学校:"+school+'\n'+"工号:"+workID; } } class Student extends People{ public String school; public int Id; public String discipline; public String grade; public String classes; public StringprintInfo(){
23、return super.printInfo()+'\n'+"学校:"+school+'\n'+"学号:"+Id+ '\n'+"专业: "+discipline+'\n'+"年纪:"+grade+'\n'+"班级:"+classes; } } class TestPerson { public static void main (String args [] ) { Teacher t=new Teacher(); t.name="Tom"; t.sex= "男"; t.bothnum= "19820808"; t.schoo
24、l= "浙江工业大学"; t.workID=2008010408; System.out.println(t.printInfo()); Student s=new Student (); s.name= "Jack"; s.sex= "男"; s.bothnum= "19920305"; s.Id=2011266302; s.school= "浙江工业大学"; s.discipline="软件工程"; s.grade= "大一"; s.classes= "2班"; System.out.println (s.printInfo(
25、)); } } 7.参考代码如下: public class 证件 { String 编号; String 姓名; String 出生年月; //格式mm/dd/yy String 部门; public 证件(){ } public 证件( String id, String name, String date,String dept ) { 编号=id; 姓名=name; 出生年月=date; //格式mm/dd/yy } public void setID( String id ){ 编号=id;
26、 } public void setName( String name ){ 姓名=name; } public void setDate( String dd ){ 出生年月=dd; } public void setDept( String dd ){ 部门=dd; } public void disp(){ System.out.println( "姓名:"+姓名+"证件号:"+编号+"出生日期"+出生年月+"所属部门"+部门); } } class 工作证 extends 证件 { String
27、 职务; String 发证日期; //格式mm/dd/yy public 工作证() { } public 工作证(String id, String name, String date,String dept,String title,String dd ) { super(id,name,date,dept); 职务=title; 发证日期=dd; } public void setTitile( String dd ){ 职务=dd; } public void set发证日期( String dd ){
28、 发证日期=dd; } public void disp(){ super.disp(); System.out.println( "职务:"+职务+"发证日期:"+发证日期); } } class 学生证 extends 证件 { String 专业; String 入校时间; //格式mm/dd/yy String 注册信息; public 学生证(){ } public 学生证(String id, String name, String date,String dept,String major,String dd,S
29、tring msg ) { super(id,name,date,dept); 专业=major; 入校时间=dd; 注册信息=msg; } public void disp(){ super.disp(); System.out.println( "专业:"+专业+"入校时间:"+入校时间+"注册信息"+注册信息); } } 8.(1)参考代码如下: interface AreaInterface ( public static final double pai=Math. PI;
30、 public abstract double area(); } (2)参考代码如下: public class Rectangle implements AreaInterface{ private double x; private double y; public Rectangle (double x,double y){ this.x=x; this.y=y; System.out.println("长方形的长:"+x+"宽为:"+y); }
31、 public double area () { return x*y; } public String toString () { return ("长方形的面积:"+this.area ()); } } (3)参考代码如下: class TestArea{ public static void main(String args[]){ Rectangle r1=new Rectangle(10.0,20.0);
32、 System.out.println(r1.toString()); } } 9.参考代码如下: public class Car { String brand; double engineDisplacement; double speed; boolean status; double maxSpeed; Car(String brand,double engineDisplacement,double maxSpeed){ this.brand=brand; this.engineDisplacement=en
33、gineDisplacement; this.maxSpeed=maxSpeed; speed=0.0; status=false; } public void start(){ status=true; System.out.println("status:"+status); } public void speedup(){ speed+=5; System.out.println("speed:"+speed); System.out.println("status:"+status); } pu
34、blic void slowdown(){ if (speed-5>=0 ){ speed-=5; } System.out.println("speed:"+speed); System.out.println("status:"+status); } public void stop(){ if (speed==0 ) status=false; System.out.println("speed:"+speed); System.out.println("status:"+status); } } 10.






