如何编写JAVA中类的继承与多态
(1)编写1个Light类,该类是对灯的描述,该类拥有:1)2个成员变量watts(私有,整型);//用于存放灯的瓦数;indicator(私有,布尔类型);//用于存放...
(1)编写1个Light类,该类是对灯的描述,该类拥有:
1) 2个成员变量
watts(私有,整型);//用于存放灯的瓦数;
indicator(私有,布尔类型);//用于存放灯的开或关的状态
2) 2构造器方法
Light(int watts) //用于创建具有watts瓦的对象
Light(int watts,boolean indicator) //用于创建具有watts瓦,开关状态为indicator的对象
3)3成员方法
public void switchOn() //开灯,即将灯的状态置为开
public void switchOff() //关灯
public void printInfo() //输出灯的瓦数信息和开关状态
(2)编写1个TubeLight类,该类是对管状灯的描述,它继承于Light类。还拥有:
1)2个成员变量
tubeLength(私有,整型) //用于存放灯管的长度
color(私有,String类型) //用于存放灯光的颜色
2) 构造器方法
TubeLight(int watts, int tubeLength,String color) //用于创建具有watts瓦,灯管长度为tugeLength,颜色为color的对象
3)成员方法
public void printInfo() //打印输出灯的相关信息,包括瓦数、开关信息、长度以及颜色
(3)请写一个测试程序,要求:
1)创建一个管状灯的实例对象,该灯瓦数为:32;长度为50;白色灯光,状态为开。
2)打印输出该灯的相关信息。
这该如何编写??? 展开
1) 2个成员变量
watts(私有,整型);//用于存放灯的瓦数;
indicator(私有,布尔类型);//用于存放灯的开或关的状态
2) 2构造器方法
Light(int watts) //用于创建具有watts瓦的对象
Light(int watts,boolean indicator) //用于创建具有watts瓦,开关状态为indicator的对象
3)3成员方法
public void switchOn() //开灯,即将灯的状态置为开
public void switchOff() //关灯
public void printInfo() //输出灯的瓦数信息和开关状态
(2)编写1个TubeLight类,该类是对管状灯的描述,它继承于Light类。还拥有:
1)2个成员变量
tubeLength(私有,整型) //用于存放灯管的长度
color(私有,String类型) //用于存放灯光的颜色
2) 构造器方法
TubeLight(int watts, int tubeLength,String color) //用于创建具有watts瓦,灯管长度为tugeLength,颜色为color的对象
3)成员方法
public void printInfo() //打印输出灯的相关信息,包括瓦数、开关信息、长度以及颜色
(3)请写一个测试程序,要求:
1)创建一个管状灯的实例对象,该灯瓦数为:32;长度为50;白色灯光,状态为开。
2)打印输出该灯的相关信息。
这该如何编写??? 展开
4个回答
展开全部
public class Light {
private int watts = 32;
private boolean indicator = true;
public Light(int watts){
this.watts=watts;
}
public Light(int watts,boolean indicator){
this.watts=watts;
this.indicator=indicator;
}
public void switchOn(){
this.indicator=true;
}
public void switchOff(){
this.indicator=false;
}
public void printInfo(){
System.out.println("watts:"+this.watts+"indicator:"+this.indicator);
}
}
class TubeLight extends Light{
private int tubeLength = 50;
private String color = "白色";
private int watts;
private boolean indicator;
public TubeLight(int watts,boolean indicator,int tubeLength,String color){
super(watts);
super(indicator);
this.watts=watts;
this.indicator = indicator;
this.color=color;
this.tubeLength=tubeLength;
}
public void printInfo(){
System.out.println("watts:"+this.watts+" indicator:"+this.indicator+" tubeLength:"+this.tubeLength+" color:"+this.color);
}
}
private int watts = 32;
private boolean indicator = true;
public Light(int watts){
this.watts=watts;
}
public Light(int watts,boolean indicator){
this.watts=watts;
this.indicator=indicator;
}
public void switchOn(){
this.indicator=true;
}
public void switchOff(){
this.indicator=false;
}
public void printInfo(){
System.out.println("watts:"+this.watts+"indicator:"+this.indicator);
}
}
class TubeLight extends Light{
private int tubeLength = 50;
private String color = "白色";
private int watts;
private boolean indicator;
public TubeLight(int watts,boolean indicator,int tubeLength,String color){
super(watts);
super(indicator);
this.watts=watts;
this.indicator = indicator;
this.color=color;
this.tubeLength=tubeLength;
}
public void printInfo(){
System.out.println("watts:"+this.watts+" indicator:"+this.indicator+" tubeLength:"+this.tubeLength+" color:"+this.color);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Light {
private int watts;
private boolean indicator;
public Light(int watts){
this.watts=watts;
}
public Light(int watts,boolean indicator){
this.watts=watts;
this.indicator=indicator;
}
public void switchOn(){
this.indicator=true;
}
public void switchOff(){
this.indicator=false;
}
public void printInfo(){
System.out.println("watts:"+this.watts+"indicator:"+this.indicator);
}
}
class TubeLight extends Light{
private int tubeLength;
private String color;
private int watts;
private boolean indicator;
public TubeLight(int watts,int tubeLength,String color){
super(watts);
this.watts=watts;
this.color=color;
this.tubeLength=tubeLength;
}
public void printInfo(){
System.out.println("watts:"+this.watts+" indicator:"+this.indicator+" tubeLength:"+this.tubeLength+" color:"+this.color);
}
}
private int watts;
private boolean indicator;
public Light(int watts){
this.watts=watts;
}
public Light(int watts,boolean indicator){
this.watts=watts;
this.indicator=indicator;
}
public void switchOn(){
this.indicator=true;
}
public void switchOff(){
this.indicator=false;
}
public void printInfo(){
System.out.println("watts:"+this.watts+"indicator:"+this.indicator);
}
}
class TubeLight extends Light{
private int tubeLength;
private String color;
private int watts;
private boolean indicator;
public TubeLight(int watts,int tubeLength,String color){
super(watts);
this.watts=watts;
this.color=color;
this.tubeLength=tubeLength;
}
public void printInfo(){
System.out.println("watts:"+this.watts+" indicator:"+this.indicator+" tubeLength:"+this.tubeLength+" color:"+this.color);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先看基础,就像帮你写了你也不明白怎么回事,还不白写?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询