Java三种对象访问方式示例请求:组合、参数传递、局部声明
Hey there! Let's walk through all three ways to work with objects in Java you're learning, with complete, runnable examples. First, let's start with a basic Temperature class since that's what you're using—this will be the foundation for all our examples:
public class Temperature { private double value; private char scale; // Constructor you referenced public Temperature(double value, char scale) { this.value = value; this.scale = scale; } // Getters to access the values (useful for our examples) public double getValue() { return value; } public char getScale() { return scale; } // Optional: Method to convert to Celsius for demonstration public double toCelsius() { if (scale == 'F') { return (value - 32) * 5/9; } return value; // Already Celsius } }
Think of this as a "has-a" relationship—one object owns another as a member variable. For example, a thermostat device logically needs a temperature reading, so we can build that ownership into the class:
public class Thermostat { // This is the composed object—Thermostat "has a" Temperature private Temperature currentTemp; // Constructor that initializes the internal Temperature public Thermostat(double initialValue, char initialScale) { this.currentTemp = new Temperature(initialValue, initialScale); } // Update the stored temperature public void updateTemperature(double newValue, char newScale) { this.currentTemp = new Temperature(newValue, newScale); } // Display the current temp converted to Celsius public void displayCurrentCelsius() { System.out.printf("Current temperature: %.2f°C%n", currentTemp.toCelsius()); } public static void main(String[] args) { // Create a Thermostat, which internally creates a Temperature object Thermostat livingRoomThermo = new Thermostat(72.0, 'F'); livingRoomThermo.displayCurrentCelsius(); // Outputs ~22.22°C // Update the temperature (replaces the internal Temperature object) livingRoomThermo.updateTemperature(25.0, 'C'); livingRoomThermo.displayCurrentCelsius(); // Outputs 25.00°C } }
You already started this with instantiating a Temperature object—let's expand it into a full example where we pass that object to a method to perform an action on it:
public class TemperatureConverter { // Method that accepts a Temperature object as input public static void printTemperatureInFahrenheit(Temperature temp) { double fahrenheitValue; if (temp.getScale() == 'C') { fahrenheitValue = (temp.getValue() * 9/5) + 32; } else { fahrenheitValue = temp.getValue(); } System.out.printf("Temperature in Fahrenheit: %.2f°F%n", fahrenheitValue); } public static void main(String[] args) { // Your original instantiation Temperature tempsF = new Temperature(32.6, 'F'); // Pass the object to our method printTemperatureInFahrenheit(tempsF); // Outputs 32.60°F // Test with a Celsius temperature too Temperature tempsC = new Temperature(0.0, 'C'); printTemperatureInFahrenheit(tempsC); // Outputs 32.00°F } }
This is when you create an object only within the scope of a method—it doesn't exist outside that method. Perfect for objects that are only needed temporarily for a specific task:
public class TempChecker { public static void checkIfFreezing() { // Local object—only exists inside this method Temperature outdoorTemp = new Temperature(31.0, 'F'); if (outdoorTemp.toCelsius() < 0) { System.out.println("It's freezing outside!"); } else { System.out.println("Above freezing."); } } public static void main(String[] args) { checkIfFreezing(); // Outputs "It's freezing outside!" // Try uncommenting the line below—you'll get a compile error! // System.out.println(outdoorTemp.getValue()); // Because outdoorTemp is local to checkIfFreezing() and not visible here } }
内容的提问来源于stack exchange,提问作者B.Allen

