下面是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-----------");
}
}