Java中Fan类toString()方法的if-else逻辑实现咨询
Got it, let's break this down step by step to create your Fan class properly. Here's how you can implement it with all the requirements:
Implementing the
Fan Java Class First, let's outline the core components we need to build a clean, functional Fan class:
- Private instance variables (following encapsulation best practices) to hold the fan's state and properties
- Constructors to initialize fan objects with default or custom values
- The overridden
toString()method that adapts its output based on whether the fan is on or off
Here's the complete, ready-to-use code:
public class Fan { // Optional but helpful: Constants for readable speed values public static final int STOPPED = 0; public static final int SLOW = 1; public static final int MEDIUM = 2; public static final int FAST = 3; // Core instance variables private boolean on; private int speed; private String color; private double radius; // Default constructor: sets sensible default values public Fan() { this.on = false; // Fan starts off by default this.speed = STOPPED; this.color = "white"; this.radius = 5.0; } // Parameterized constructor: lets you create a fan with custom settings public Fan(boolean on, int speed, String color, double radius) { this.on = on; // Validate speed to keep it within valid range this.speed = (speed >= STOPPED && speed <= FAST) ? speed : STOPPED; this.color = color; this.radius = radius; } // Getters and Setters (encapsulation control) public boolean isOn() { return on; } public void setOn(boolean on) { this.on = on; // Automatically set speed to stopped when turning the fan off if (!on) { this.speed = STOPPED; } } public int getSpeed() { return speed; } public void setSpeed(int speed) { // Only allow speed changes if the fan is on if (this.on) { this.speed = (speed >= STOPPED && speed <= FAST) ? speed : STOPPED; } } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } // Override toString() to match your requirements @Override public String toString() { if (on) { // Return speed, color, radius when fan is on return String.format("Fan is active - Speed: %d, Color: %s, Radius: %.2f", speed, color, radius); } else { // Return color, radius + "fan is off" when fan is off return String.format("Fan status: off - Color: %s, Radius: %.2f, fan is off", color, radius); } } // Test the class with a main method public static void main(String[] args) { Fan bedroomFan = new Fan(true, Fan.FAST, "black", 7.5); System.out.println(bedroomFan); // Outputs on-state message bedroomFan.setOn(false); System.out.println(bedroomFan); // Outputs off-state message } }
Key Details Explained:
- Speed Constants: Using named constants like
FASTinstead of raw integers makes the code easier to read and maintain. You can skip this if you prefer a simpler setup, but it's a common best practice. - Encapsulation: All variables are
private, with getters/setters to control how they're accessed. For example,setSpeed()only allows changes if the fan is on, which aligns with real-world logic. - toString() Logic: The method checks the
onflag first. If true, it builds a string with speed, color, and radius. If false, it includes color, radius, plus the required "fan is off" phrase.String.format()ensures clean, formatted output, but you could also use string concatenation if you prefer. - Constructor Validation: The parameterized constructor checks that the speed is within a valid range, preventing invalid values from being set accidentally.
When you run the main method, you'll get output like this:
Fan is active - Speed: 3, Color: black, Radius: 7.50
Fan status: off - Color: black, Radius: 7.50, fan is off
内容的提问来源于stack exchange,提问作者jamesdamico
相关产品推荐
相关产品推荐

