第五章 1. 答:参阅教材5.1.1节内容。
2. 答:Object类是所有类的根类,其中定义了11个方法,它们被子类继承。
3. 答:在子类中定义与超类中的名字、参数列表、返回值类型都相同的方法,这时子类的方法就叫做覆盖。在同一个类中定义的名称相同、参数个数或类型不同的方法称为方法重载。
4. 答:(1)在子类中访问超类中被隐藏的成员变量。(2)在子类中调用超类中被覆盖的方法。(3)在子类中调用超类的构造方法。
5. 答:能。
6. 答: final修饰符可以修饰类、方法和变量。abstract修饰符可以修饰类和方法。
7. 答:对象类型转换分为自动类型转换和强制类型转换。
8. 答:缺省和public。缺省访问修饰符的类只能被同一个包中的类访问,public修饰符的类可以被任何类访问。
9. 答:类成员的访问修饰符有:public、protected、缺省和private。
10. 答:抽象类中可以定义非抽象方法,接口中不可以。
11. 答:接口的实现使用implements关键字,即实现接口中定义的抽象方法。注意,接口中定义的方法修饰符都是public。
12. 答:Java语言允许定义内部类的目的是增强两个类之间的关系。内部类被编译成单独的类文件,名称为:外部类名$内部类名.class。
13. 答:E
14. 答:在BB类的构造方法中将调用AA类默认的构造方法,而AA类没有提供默认的构造方法。
15. 输出结果:
m = 1000
m = 480
16. 答:A子类的构造方法中没有调用超类的构造方法,故自动调用超类的默认构造方法,而超类中又没有定义默认的构造方法,找不到Super(),产生编译错误。
17. 答:B
18. 答:方法覆盖时不能使用更低的访问权限,应去掉getFloat()方法的private修饰符。
19. 答:A, D
20. 答:
I am parent
I am child
I am child
21. 程序输出:
Inside BB's callme().
Inside BB's metoo().
22. 答:D 23. 答:A
24. 输出结果为:
He is a Manager.
He is a Secretary.
He is a Programmer.
25. 答:
(1)抽象类不能实例化。去掉IamAbstract类的abstract修饰符。
(2)若方法定义为抽象方法,类应该定义为抽象类。将method()方法的abstract修饰符去掉,或在IamAbstract类上添加abstract。
26. 答:B 27. 答:C, E
28. 参考程序如下:
public class Student extends Person{
String sno;
String major;
public void setSno(String sno){
this.sno = sno;
}
public void setMajor(String major){
this.major = major;
}
public String getSno(){
return sno;
}
public String getMajor(){
return major;
}
public static void main(String[] args){
Student stud = new Student();
stud.setName("LiuMing");
stud.setAge(20);
stud.setSno("200215121");
stud.setMajor("Information Science");
System.out.println(stud.getName());
System.out.println(stud.getAge());
System.out.println(stud.getSno());
System.out.println(stud.getMajor());
}
}
29. 参考程序如下:
public class Auto{
private double speed;
public void start(){
System.out.println("The auto is started.");
}
public void speedUp(double speed){
this.speed = speed;
System.out.println("The auto is speed up to "+speed+"kilo/h.");
}
public void stop(){
this.speed = 0;
System.out.println("The auto is stoped.");
}
}
public class Bus extends Auto{
private int passenger;
public void gotOn(int n){
passenger = passenger+n;
System.out.println("The person on bus is:"+passenger);
}
public void gotOff(int n){
passenger = passenger-n;
System.out.println("The person on bus is:"+passenger);
}
}
public class BusTest{
public static void main(String []args){
Bus bus = new Bus();
bus.start();
bus.speedUp(60);
bus.stop();
bus.gotOn(10);
bus.gotOff(5);
}
}
30. 参考程序如下:
public abstract class Shape{
private String name;
public Shape(){}
public Shape(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public abstract double perimeter();
public abstract double area();
}
public class Triangle extends Shape{
double a,b,c;
public Triangle(){
this.a = 0; this.b = 0; this.c = 0;
}
public Triangle(double a, double b, double c){
this.a = a; this.b = b; this.c = c;
}
public double area(){
double s = (a + b + c) / 2.0;
return Math.sqrt(s * (s-a) * (s-b) * (s-c));
}
public double perimeter(){
return a + b + c;
}
public static void main(String[] args){
Triangle ta = new Triangle(3, 4, 5);
System.out.println(ta.area());
}
}
31. 参考程序如下:
public class Cuboid extends Rectangle{
private double height;
public Cuboid(double length,double width, double height){
super(length,width);
this.height = height;
}
public Cuboid(){
this(0,0,0);
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
public double volume(){
return area()*height;
}
}
public class CuboidTest{
public static void main(String[]args){
Cuboid cb = new Cuboid();
cb.setLength(10);
cb.setWidth(5);
cb.setHeight(2);
System.out.println("volume="+cb.volume());
}
}
32. 参考程序如下:
public abstract class CompareObject{
public abstract int compareTo(Object obj);
}
public class Position extends CompareObject{
private int x;
private int y;
public Position(){}
public Position(int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX(){return x;}
public int getY(){return y;}
//实现compareTo()方法
public int compareTo(Object obj){
Position pos = (Position)obj;
double dist1 = Math.sqrt(x*x + y*y);
double dist2 = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
return (int)(dist1-dist2);
}
public static void main(String[]args){
Position pos1 = new Position(0,0);
Position pos2 = new Position(3,4);
System.out.println(pos1.compareTo(pos2));
}
}
33. 设计一个日期类MyDate,要求构造MyDate类的对象时,如果没有给出参数,则日期设定为1970年1月1日,如果给出参数,则需要给出年月日。编写一个equals()方法判断两个日期是否相等;另一个方法compareTo()可以进行日期的比较,该方法返回两个日期相差的天数。
34. 答:
(1)① ⑤
(2)② ⑤
(3)① 3 ② 5 ③ 无 ④ 4 ⑤ 无 ⑥ 5
35.
有下列事物:汽车,玩具汽车,玩具飞机,阿帕奇直升机。请按照它们之间的关系,使用接口和抽象类,编写出有关代码。
36. 参考程序:
public enum TrafficLight{
GREEN, RED, YELLOW;
public static void main(String[]args){
TrafficLight[] tl = TrafficLight.values();
for(TrafficLight light:tl){
System.out.print(light);
System.out.println(" "+light.ordinal());
}
TrafficLight red = TrafficLight.RED;
switch(red){
case RED:
System.out.println("RED, stop."); break;
case GREEN:
System.out.println("GREEN, go."); break;
case YELLOW:
System.out.println("YELLOW, do not know."); break;
}
}
}
37. 答:注解类型的定义如下。
public @interface Author{
String name();
String date();
}
38. 参考程序如下:
public interface Flyable{
public abstract void takeoff();
public abstract void land();
public abstract void fly();
}
public abstract class Vehicle {
}
public abstract class AirPlane extends Vehicle implements Flyable{
public void takeoff(){
System.out.println("Plane takeoff.");
}
public void land(){
System.out.println("Plane land.");
}
public void fly(){
System.out.println("Plane flying.");
}
}
public abstract class Animal{
public abstract void eat();
}
public class Bird extends Animal implements Flyable{
public void takeoff(){
System.out.println("Bird takeoff.");
}
public void land(){
System.out.println("Bird land.");
}
public void fly(){
System.out.println("Bird flying.");
}
public void eat(){
System.out.println("Bird eating.");
}
public void buildNest(){
System.out.println("Bird buiding nest.");
}
public void layEggs(){
System.out.println("Bird lying eggs.");
}
}
public class Superman extends Animal implements Flyable{
public void takeoff(){
System.out.println("Superman takeoff.");
}
public void land(){
System.out.println("Superman land.");
}
public void fly(){
System.out.println("Superman flying.");
}
public void eat(){
System.out.println("Superman eating.");
}
public void leapBuiding(){
System.out.println("Superman leap building.");
}
public void stopBullet(){
System.out.println("Superman stop bullet.");
}
}
public class PolymorphismDemo{
public static void main(String []args){
Flyable plane = new AirPlane();
plane.takeoff();
plane.fly();
Flyable bird = new Bird();
bird.fly();
bird.buildNest(); // 编译错误
}
}
39. 答:(1)若将乐器都定义为类,则子类可以继承或覆盖超类中的方法。(2)若将Instrument定义成抽象类,则子类需要实现Instrument类的方法。(3)若将Instrument定义为接口,Wind定义为抽象类,其他定义为具体类。实现代码如下:
public interface Instrument{
public abstract void play();
public abstract String what();
public abstract void adjust();
}
public abstract class Wind implements Instrument{
}
public class Woodwind extends Wind{
public void play(){
System.out.println("Woodwind playing.");
}
public String what(){
return "Woodwind";
}
public void adjust(){
System.out.println("Woodwind adjusting.");
}
}
public class Brass extends Wind{
public void play(){
System.out.println("Brass playing.");
}
public String what(){
return "Brass";
}
public void adjust(){
System.out.println("Brass adjusting.");
}
}
public class Percussion implements Instrument{
public void play(){
System.out.println("Percussion playing.");
}
public String what(){
return " Percussion ";
}
public void adjust(){
System.out.println("Percussion adjusting.");
}
}
public class Stringed implements Instrument{
public void play(){
System.out.println("Stringed playing.");
}
public String what(){
return "Stringed";
}
public void adjust(){
System.out.println("Stringed adjusting.");
}
}
public class InstrumentDemo{
public static void main(String []args){
Instrument ins = new Brass();
ins.play();
ins.adjust();
System.out.println(ins.what());
}
}
来源:网络整理 免责声明:本文仅限学习分享,如产生版权问题,请联系我们及时删除。
相关文章:
知识产权法综合练习题(3)04-30
英语I(1)导学资料(二)04-30
电大《影视鉴赏》课程资料04-30
开放教育电大《小城镇建设》课程综合练习题课程论文样04-30
知识产权法综合练习题(4)04-30
电大《劳动法学》201201期末考试试题04-30
知识产权法综合练习题(2)04-30
宪法学综合练习题304-30
公司财务报表分析练习04-30
宪法学综合练习题104-30