9.8(Fan类)设计一个名为Fan的类来表示风扇。这个类包含:

下面是Java的源代码:

class Fan{

    final int SLOW = 1;
    final int MEDIUM = 2;
    final int FAST = 3;
    private int speed;
    private Boolean on ;
    private double radius;
    public String color ;

    Fan(){
        speed=SLOW;
        on = false;
        radius = 5;
        color = "blue";
    }

    public String toString(){
        if(on==false)
            return "fan is off "+"fan is "+color+" and its radius is "+radius;
        else
            return "fan's speed is " +speed+" fan's color is " + color+" fan's radius is"+radius;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Boolean getOn() {
        return on;
    }

    public void setOn(Boolean on) {
        this.on = on;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
public class Fan类 {

    public static void main(String[] args) {
        System.out.println("--------------test---------------");

        Fan f1 =new Fan();

        System.out.println(f1.toString());

        f1.setOn(true);

        System.out.println(f1.toString());

        System.out.println("--------------over---------------");

        Fan ob1 = new Fan();

        System.out.println();

        System.out.println("---------------obj1---------------");

        ob1.setSpeed(ob1.FAST);

        ob1.setColor("yellow");

        ob1.setRadius(10);

        ob1.setOn(true);

        System.out.println(ob1.toString());

        System.out.println("---------------obj1over-----------");

        Fan ob2 = new Fan();

        System.out.println();

        System.out.println("---------------obj2---------------");

        ob1.setSpeed(ob2.MEDIUM);

        ob1.setColor("blue");

        ob1.setRadius(5);

        ob1.setOn(false);

        System.out.println(ob1.toString());

        System.out.println("---------------obj2over-----------");

    }

}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值