栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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-----------");

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/355924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号