日期:2014-05-20  浏览次数:20865 次

java的继承问题
(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)打印输出该灯的相关信息。



------解决方案--------------------
好好试着写,在java中太简单了
------解决方案--------------------
类的编码自己搞定吧。对于要求可以说明几点:
1)创建一个管状灯的实例对象,该灯瓦数为:32;长度为50;白色灯光,状态为开。
:赋值可以用带参数构造方法
2)打印输出该灯的相关信息。
:重写toString方法
------解决方案--------------------
个人觉得Light类的成员变量应该为protect类型的,因为TubeLight类继承Light并且用到这些变量。代码如下:
Java code

class Light
{
    protected int watts;//存放灯的瓦数
    
    protected boolean indicator;//用于标识开关
    
    Light (int watts){//含有一个参数的构造方法
        this.watts = watts;
    }
    
    Light(int watts, boolean indicator){//含有两个参数的构造方法
        this.indicator = indicator;
        this.watts = watts;
    }
    
    public void switchOn(){ //开灯,即将灯的状态置为开
        indicator = true;
    }
    
      public void switchOff(){ //关灯
          indicator = false;
      }
      
      public void printInfo(){ //输出灯的瓦数信息和开关状态
          System.out.println("the watts: "+watts+".");
          System.out.println("the indicator: "+indicator+".");
      }
}

class TubeLight extends Light
{
    private int tubeLight;//用于存放灯管儿的长度
    
    private String color;//用于存放灯管儿的颜色
    
    TubeLight(int watts, int tubeLight, String color)//带三个参数的的构造方法
    {
        super(watts);
        this.tubeLight = tubeLight;
        this.color = color;
    }
    
    public void printInfo(){ //输出灯的瓦数信息和开关状态
          super.printInfo();
          System.out.println("the tubeLight: "+tubeLight);
          System.out.println("the color: "+color);
      }
}

public class Test{//测试类
    public static void main(String[] args)
    {
        Light light = new TubeLight(32, 50, "white");
        
        light.switchOn();
        
        light.printInfo();
    }
}

------解决方案--------------------
Java code

public class Light {
    private int watts;   //(私有,整型);//用于存放灯的瓦数;
    private boolean indicator;//(私有,布尔类型);//用于存放灯的开或关的状态
    public Light(int watts){
        this.watts=watts;
    } //用于创建具有watts瓦的对象
    public Light(int watts,boolean indicator){
        this.watts=watts;
        this.indicator=indicator;
    } //用于创建具有watts瓦,开关状态为indicator的对象
      public void switchOn(){
          this.indicator=true;
      } //开灯,即将灯的状态置为开
      public void switchOff(){
          this.indicator=false;
      } //关灯
      public void printInfo(){
          System.out.println(this.watts+" "+this.indicator);
      } //输出灯的瓦数信息和开关状态
}