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()); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)