当前位置:文档之家› 第五章 类与继承作业一

第五章 类与继承作业一

第五章类与继承作业一
一、填空选择题
1、(在同一包中)子类不能继承父类中的私有成员,除此之外,其它所有的成员都可以通过继承变为子类的成员。

2、给出如下的程序,A-E选项中与构造方法ConstOver重载的两个构造方法是:AC。

.public class ConstOver {
publicConstOver (int x, int y, int z) { }
}
A.ConstOver ( ) { }
B.ProtectedintConstOver ( ) { }
C.PrivateConstOver (int z, int y, byte x) { }
D.public Object ConstOver (int x, int y, int z) { }
E.public void ConstOver (byte x, byte y, byte z) { }
3、已知类关系如下:
class Employee;
class Manager extends Employeer;
class Director extends Employee;
则以下关于数据的语句正确的是:(A)。

A. Employee e=new Manager();
B. Director d=new Manager();
C. Director d=new Employee();
D. Manager m=new Director();
4、(A)下面对类的申明哪个是正确的?
A)public class Fred {
publicint x = 0;
public Fred (int x) {
this.x = x;
}
}
B)public class fred
publicint x = 0;
publicfred (int x) {
this.x = x;
}
}
C)public class Fred extends MyBaseClass, MyOtherBaseClass {
publicint x = 0;
public Fred (intxval) {
x = xval;
}
}
D)protected class Fred {
privateint x = 0;
private Fred (intxval) {
x = xval;
}
5、(B)下面程序中类ClassDemo中定义了一个静态变量sum,分析程序段的输出结果。

classClassDemo {
public static int sum=1;
publicClassDemo()
{sum=sum+5;}
}
classClassDemoTest{
public static void main(String args[]) {
ClassDemo demo1=new ClassDemo();
ClassDemo demo2=new ClassDemo();
System.out.println(demo1.sum);
}
}
A) 0 B) 6 C) 11 D) 2
二、编程题
1、编写1个Light类,该类是对灯的描述,该类拥有:
(1)2个成员变量
watts(私有,整型);//用于存放灯的瓦数;
indicator(私有,布尔类型);//用于存放灯的开或关的状态
(2)2构造器方法
Light(int watts) //用于创建具有watts瓦的对象
Light(intwatts,boolean indicator) //用于创建具有watts瓦,开关状态为indicator的对象
(3)3成员方法
public void switchOn() //开灯,即将灯的状态置为开
public voidswitchOff() //关灯
public void printInfo() //输出灯的瓦数信息和开关状态
2、编写1个TubeLight类,该类是对管状灯的描述,它继承于Light类。

还拥有:
(1)2个成员变量
tubeLength(私有,整型) //用于存放灯管的长度
color(私有,String类型) //用于存放灯光的颜色
(2)构造器方法
TubeLight(int watts, inttubeLength,String color) //用于创建具有watts 瓦,灯管长度为tugeLength,颜色为color的对象
(3)成员方法
public void printInfo() //打印输出灯的相关信息,包括瓦数、开关信息、长度以及颜色
3、请写一个测试程序,要求:
(1)创建一个管状灯的实例对象,该灯瓦数为:32;长度为50;白色灯光,状态为开。

(2)打印输出该灯的相关信息。

public class Light{
privateint watts=0;
privateboolean indicator=false;
public Light(int watts){
this.watts=watts;
}
public Light(intwatts,Boolean indicator){
this.watts=watts;
dicator=indicator;
}
public void switchOn(){
dicator=true;
}
public void switchOff(){
dicator=false;
}
public void printInfo() {
intln("watts:" + this.watts);
intln("indicator:" + dicator);
} }
public classTubeLight{
privateinttubeLength=0;
private String color=””;
public TubeLight(int watts, inttubeLength,String color){ super(watts);
this.tubeLength=tubelength;
lor=color;
}
public void printInfo() {
intInfo();
intln("tubeLength:" + this.tubeLength);
intln("color:" + lor);
} }
public class test {
/**
* @paramargs
*/
public static void main(String[] args) {
// TEST
TubeLighttubeLight = new TubeLight(32, 50, "white");
tubeLight.switchOn();
intInfo();}。

相关主题