编写1个 TubeLight类,该类是对管状灯的描述,它继承于 Light类。
要求:
1)2个成员变量
tubeLength(私有,整型) //用于存放灯管的长度
color(私有, String类型) //用于存放灯光的颜色
2)构造函数
TubeLight (int watts, int tubeLength, String color)
用于创建具有 watts瓦,灯管长度为 tugeLength,颜色为color的对象
3)成员方法
public void printInfo()打印输出灯的相关信息 //包括瓦数、开关信息、长度以及颜色
4)请写一个测试程序,要求
①创建一个管状灯的实例对象,该灯瓦数为:32;长度为50;白色灯光,状态为开。
②打印输出该灯的相关信息。
父类:
public class Light {
protected int watts; //灯的瓦数
protected String state;
public int getWatts() {
return watts;
}
public void setWatts(int watts) {
this.watts = watts;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Light() {
super();
// TODO Auto-generated constructor stub
}
public Light(int watts, String state) {
super();
this.watts = watts;
this.state = state;
}
public void printlnfo() {
System.out.print("瓦数:"+watts+" 开关信息:"+state);
}
}
子类:
public class TubeLight extends Light{
private int tubeLength; //灯管长度
private String color; //颜色
public int getTubeLength() {
return tubeLength;
}
public void setTubeLength(int tubeLength) {
this.tubeLength = tubeLength;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public TubeLight(int watts, String state,int tubeLength, String color) {
super(watts, state);
// TODO Auto-generated constructor stub
this.tubeLength=tubeLength;
this.color=color;
}
public TubeLight() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void printlnfo() {
// TODO Auto-generated method stub
super.printlnfo();
System.out.print(" 灯管长度:"+tubeLength+" 颜色:"+color);
}
}
测试类:
public class LightTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TubeLight tubeLight=new TubeLight();
tubeLight.watts=32;
tubeLight.setColor("白色");
tubeLight.setTubeLength(50);
tubeLight.state="开启";
tubeLight.printlnfo();
System.out.println();
TubeLight tubeLight2=new TubeLight(32,"开启",50,"白色");
tubeLight.printlnfo();
}
}
运行结果:
瓦数:32 开关信息:开启 灯管长度:50 颜色:白色
瓦数:32 开关信息:开启 灯管长度:50 颜色:白色