当前位置:首页 > java程序设计项目教程第3章答案
一、选择题 参考答案:
1.B 2.A 3.D 4.C 5.A 6.A 7.B 8.B 9.A 10.A 11.A 12.D 二、填空题
参考答案:
1.覆盖 2.参数 3.方法体 4.public static final 5.抽象 6.extends 7.Student String 8.class static 三、简答题
1.子类能够继承父类的哪些成员变量和方法?
①子类可以继承父类的属性,包括实例成员变量和类成员变量。
②子类可以继承父类除构造方法以外的成员方法,包括实例成员方法和类成员方法。 2.this和super关键字的作用是什么?
使用this关键字可实现调用本类的构造方法及调用被方法隐藏的成员变量。
super关键字可调用父类的构造方法及调用父类的同名成员。 3.什么是方法的重载?什么是方法的覆盖?
方法重载是指同一个类中多个方法享有相同的名字,但是这些方法的参数必须不同,参数不同是,指或者是参数的个数不同,或者是参数类型不同,或者是不同类型参数的排列顺序不同。
类继承的过程中,子类方法跟父类方法名字相同,并且传递的参数完全一样,称子类覆盖了父类的方法。
4.什么是多态?使用多态有什么优点?
多态的表现形式主要有方法的重载和方法的覆盖。
使用多态可根据同名方法的参数不同实现不能的功能或同一类对象根据赋予的引用对象的不同来实现不同的行为。
5.什么是包?定义包的作用是什么?
包是java提供的一种命名空间机制,实现了对类的存放和引用位置的管理,包对应一个文件夹。
java的类库就是用包来实现了类的分类和存放,每个包中都有多组相关的类和接口。 6.什么是接口?接口中的变量和方法各有什么要求?
接口是特殊的抽象类,可以想象为一个“纯”抽象类,就是一组具有特定意义的静态常量和抽象方法的集合。
属性定义时必须赋初值,是常量。属性性前修饰符时默认该属性由final、static修饰。 接口中的方法必须是抽象方法。
7.类BB是AA类的子类。类AA和类BB中都定义了变量x和method()方法,这种情况称为子类隐藏了父类的同名变量x并覆盖了父类的method()方法. 8.输出结果为: AA BB
若将main()方法中的语句改为:BB b=new BB(10); 程序输出的结果是什么? AA AAAA BBBB
四、编程
1.编写一个类,描述学生的学号、姓名、成绩。学号用整型,成绩用浮点型,姓名用String类型。编写一个测试类,输入学生的学号和成绩,并显示该学号的学生姓名,以及成绩。 参考答案: class Student{ int sNum; double score; String name; }
public class T1_Student{ public static void main(String[] args){ Student s=new Student(); s.sNum=101; s.score=97; s.name=\张三\ System.out.println(s.name+\的学号是\成绩是\ } }
2.编写测试程序,首先创建一个Student对象,利用setName()方法设置xm属性(设成自己的名字),利用setBj()方法设置bj属性(设成所在班级),然后输出自己的名字和班级,运行这个测试程序查看输出结果。 参考答案: class Student {
String bj,xm;
void setXm(String x) { xm=x; }
void setBj(String y) { bj=y; }
public static void main(String args[]) { Student st=new Student(); st.setXm(\ st.setBj(\ System.out.print(\ } }
3.编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main方法中输出修改过后的汽车信息。
参考答案:
public class ChangeCar{ public static void main(String[] args){ Car c=new Car(); c.carNum=\辽B1111\ c.price=10; System.out.println(\ changePrice(c); System.out.println(\ } public static void changePrice(Car c){ c.price=c.price*0.8; } } class Car{ String carNum; double price;
}
4.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。 参考答案: class Vehicle {
int wheels; float weight;
Vehicle(int a,float b) { wheels=a; weight=b; }
void outData() {
System.out.print(\ } }
class Car extends Vehicle {
int loader;
Car(int a,float b,int c) { super(a,b); loader=c;
}
void outData() {
super.outData();
System.out.print(\ } }
class Truck extends Car {
float payload;
Truck(int a,float b,int c,float d) { super(a,b,c); payload=d; }
void outData() {
super.outData();
System.out.println(\ } }
class Test {
public static void main(String args[]) { Vehicle v=new Vehicle(4,7); v.outData(); System.out.println(); Car c=new Car(4,6,4); c.outData(); System.out.println(); Truck t=new Truck(4,2,10,8); t.outData(); } } 5.定义一个接口CanFly,描述会飞的方法public void fly();分别定义类飞机和鸟,实现CanFly接口。定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,让飞机和鸟起飞。 参考答案: interface CanFly { public void fly(); }
class Plane implements CanFly
{ public void fly() { System.out.println(\飞机需借助螺旋桨飞上了天空。\ } }
class Bird implements CanFly { public void fly() { System.out.println(\鸟需借助翅膀飞上了天空。\ } }
class Test { public static void main(String args[]) { Plane p=new Plane(); Bird b=new Bird(); p.fly(); b.fly(); } }
共分享92篇相关文档