Java面向对象的练习

Java面向对象的练习,第1张

Java面向对象的练习
package FanTest;

public class FanTest {
    public static void main(String[] args) {
        Fan f1=new Fan();
        Fan f2=new Fan();
        f1.setSpeed(3);
        f1.setOn(true);
        f1.setColor("yellow");
        f1.setRadius(10);
        f1.tostring(f1);
        f2.setRadius(0);
        f2.setOn(false);
        f2.tostring(f2);
    }
}
class Fan{
    private int speed=1;
    private boolean on=false;
    private double radius=0.0;
    private String color="blue";

    public Fan(){}
    //四个属性的访问器
    public void setSpeed(int speed){
        this.speed=speed;
    }
    public void setOn(boolean on){
        this.on=on;
    }
    public void setRadius(double radius){
        this.radius=radius;
    }
    public void setColor(String color){
        this.color=color;
    }
    //四种属性的修改器
    public int getSpeed(){
        return speed;
    }
    public boolean geton(){
        return on;
    }
    public double getRadius(){
        return radius;
    }
    public String getColor(){
        return color;
    }
    public void tostring(Fan f) {
        String a;
        if (speed == 1) {
            a = "slow";
        }
            else if(speed==2) {
            a = "medium";
        }
            else {
            a = "fast";
        }
            if (on == false) {
                System.out.println("速度为:"+a+" 状态为:"+"fan is off, " +"颜色为:"+f.getColor()+ ",半径为:" + f.getRadius());
            } else {
             System.out.println("速度为:"+a+" 半径为:"+f.getRadius()+" 颜色为:"+f.getColor());
            }
        }
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5612725.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存