Java中嵌套类的使用及Lab21_Vars代码错误修正问询
Hey there! Let's break down your questions into two parts: first, how to implement nested classes in Java, then fixing up the issues in your Lab21_Vars code.
Java supports four types of nested classes, each with distinct use cases and features:
1. Member Inner Class
This is a non-static class defined inside an outer class. It depends on an instance of the outer class to be created, and can directly access all members of the outer class (including private ones).
public class OuterClass { private String outerMsg = "Hello from Outer Class!"; // Member inner class class InnerClass { public void showOuterMsg() { System.out.println(outerMsg); // Directly access outer class's private member } } public static void main(String[] args) { // Must create outer class instance first, then inner class instance OuterClass outer = new OuterClass(); OuterClass.InnerClass inner = outer.new InnerClass(); inner.showOuterMsg(); // Output: Hello from Outer Class! } }
2. Static Nested Class
A nested class marked with static, it belongs to the outer class itself and doesn't require an outer class instance to be created. It can only access static members of the outer class.
public class OuterClass { private static String staticOuterMsg = "Hello from Static Outer!"; // Static nested class public static class StaticNestedClass { public void showStaticMsg() { System.out.println(staticOuterMsg); // Only access outer class's static members } } public static void main(String[] args) { // Create static nested class instance directly, no outer class object needed OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass(); nested.showStaticMsg(); // Output: Hello from Static Outer! } }
3. Local Inner Class
A class defined inside a method or code block, it's only visible within that scope. It can access final or effectively final local variables from its containing scope.
public class OuterClass { public void createLocalClass() { final String localMsg = "Hello from Local Scope!"; // Local inner class, only visible in this method class LocalInnerClass { public void showLocalMsg() { System.out.println(localMsg); } } LocalInnerClass localInner = new LocalInnerClass(); localInner.showLocalMsg(); // Output: Hello from Local Scope! } public static void main(String[] args) { new OuterClass().createLocalClass(); } }
4. Anonymous Inner Class
A local inner class without a name, commonly used to quickly implement an interface or extend a class for one-time use cases.
public class OuterClass { interface Greeting { void sayHello(); } public static void main(String[] args) { // Anonymous inner class implementing Greeting interface Greeting greeting = new Greeting() { @Override public void sayHello() { System.out.println("Hello from Anonymous Inner Class!"); } }; greeting.sayHello(); // Output: Hello from Anonymous Inner Class! } }
Here's the fully corrected code, followed by explanations for each fix and the completed statement:
import java.util.*; public class Lab21_Vars { public static void main(String[] args) { int i = 0; int j = 0; int var0, var1; // Fix 1: Correctly typecast 5.0 to an int so it can assigned to var3. int var2 = 0, var3 = (int) 5.0; // Fix 2: Correctly declare a variable as an array. int[] arri0 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; double[] arrd0 = {0.0, 1.0, 2.0, 3.0, 4.0}; // Fix 3: What's wrong with temp? Fix it. int temp = 0; if (i < j) { System.out.println("Temp is " + temp); } else { System.out.println("Temp is still " + temp); } } }
Validation of Each Fix
- Fix 1: This fix is completely correct.
5.0is a double type, and Java doesn't allow implicit conversion from floating-point to integer types. Using(int)for explicit typecasting truncates the floating-point value to an integer (resulting in 5 here), making it assignable to the int variablevar3. - Fix 2: This fix is also correct. The standard Java syntax for array declaration is
type[] variableName(whiletype variableName[]is legal, the former is more readable). Theint[] arri0syntax follows this standard, and initializing the array with a brace-enclosed list is a valid initialization method. - Fix 3: This fix is correct as well. Java requires local variables to be explicitly initialized before use. Without initializing
temp, entering the else branch (which happens here sinceiandjare both 0) would cause a compilation error. Initializingtempto 0 resolves this issue.
Completed System.out Statement
The original System.ou... was a typo. I completed it to System.out.println("Temp is still " + temp) to ensure the else branch prints a meaningful message. You can adjust the output text as needed for your use case.
内容的提问来源于stack exchange,提问作者Ulyses

