本文目录导读:

我来为您详细介绍Java内部类的案例编写,包括各种内部类的典型用法。
成员内部类
// 外部类
public class OuterClass {
private String message = "外部类的私有成员";
// 成员内部类
public class InnerClass {
private String innerMessage = "内部类的成员";
public void display() {
// 内部类可以访问外部类的私有成员
System.out.println("访问外部类: " + message);
System.out.println("访问内部类: " + innerMessage);
}
// 获取外部类引用
public void getOuterReference() {
// 通过 外部类名.this 访问外部类实例
System.out.println(OuterClass.this.message);
}
}
// 外部类方法,创建内部类实例
public void createInner() {
InnerClass inner = new InnerClass();
inner.display();
}
public static void main(String[] args) {
// 创建外部类实例
OuterClass outer = new OuterClass();
// 方式1:通过外部类方法创建
outer.createInner();
// 方式2:直接创建内部类实例
OuterClass.InnerClass inner = outer.new InnerClass();
inner.display();
}
}
静态内部类
public class StaticOuterClass {
private static String staticMessage = "静态外部类消息";
private String instanceMessage = "实例外部类消息";
// 静态内部类
public static class StaticInnerClass {
private String innerMessage = "静态内部类消息";
public void display() {
// 只能访问外部类的静态成员
System.out.println("访问静态外部类: " + staticMessage);
System.out.println("访问内部类: " + innerMessage);
// 错误:不能访问外部类的实例成员
// System.out.println(instanceMessage); // 编译错误
}
// 可以包含静态成员
public static void staticMethod() {
System.out.println("静态内部类的静态方法");
}
}
public static void main(String[] args) {
// 创建静态内部类实例(不需要外部类实例)
StaticInnerClass inner = new StaticInnerClass();
inner.display();
// 调用静态方法
StaticInnerClass.staticMethod();
}
}
局部内部类
public class LocalInnerClass {
public void demonstrateLocalClass() {
// 局部变量(必须是 final 或 effectively final)
String localVar = "局部变量";
int number = 42;
// 局部内部类(定义在方法内)
class LocalClass {
private String localInnerVar = "局部内部类变量";
public void display() {
// 访问外部类的局部变量
System.out.println("局部变量: " + localVar);
System.out.println("数字: " + number);
System.out.println("内部类变量: " + localInnerVar);
}
}
// 创建局部内部类实例
LocalClass local = new LocalClass();
local.display();
}
public void anotherMethod() {
// 错误:不能在其他方法中使用局部内部类
// LocalClass local = new LocalClass(); // 编译错误
}
public static void main(String[] args) {
LocalInnerClass outer = new LocalInnerClass();
outer.demonstrateLocalClass();
}
}
匿名内部类
// 接口定义
interface Greeting {
void sayHello();
void sayGoodbye();
}
// 抽象类定义
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("动物在吃东西");
}
}
public class AnonymousInnerClass {
public void demonstrateAnonymous() {
// 1. 基于接口的匿名内部类
Greeting greeting = new Greeting() {
@Override
public void sayHello() {
System.out.println("你好!");
}
@Override
public void sayGoodbye() {
System.out.println("再见!");
}
};
greeting.sayHello();
greeting.sayGoodbye();
// 2. 基于抽象类的匿名内部类
Animal cat = new Animal() {
@Override
void makeSound() {
System.out.println("喵喵~");
}
// 可以添加额外的方法,但只能通过匿名类内部访问
void purr() {
System.out.println("咕噜咕噜...");
}
};
cat.makeSound();
cat.eat();
// 3. 基于普通类的匿名内部类
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("匿名线程执行中...");
}
};
thread.start();
// 4. 作为方法参数使用
processButton(new Runnable() {
@Override
public void run() {
System.out.println("按钮被点击!");
}
});
}
private void processButton(Runnable action) {
System.out.println("处理按钮事件前...");
action.run();
System.out.println("处理按钮事件后...");
}
public static void main(String[] args) {
AnonymousInnerClass demo = new AnonymousInnerClass();
demo.demonstrateAnonymous();
}
}
综合案例:计算器
import java.util.ArrayList;
import java.util.List;
public class Calculator {
private List<String> history = new ArrayList<>();
// 1. 成员内部类:加法操作
public class Addition {
public int calculate(int a, int b) {
int result = a + b;
logOperation(a + " + " + b + " = " + result);
return result;
}
}
// 2. 静态内部类:乘法操作
public static class Multiplication {
public int calculate(int a, int b) {
int result = a * b;
// 静态内部类只能访问外部类的静态成员
System.out.println(a + " * " + b + " = " + result);
return result;
}
}
// 3. 局部内部类:减法操作
public int subtract(int a, int b) {
class Subtraction {
public int subtract(int x, int y) {
return x - y;
}
}
Subtraction sub = new Subtraction();
int result = sub.subtract(a, b);
logOperation(a + " - " + b + " = " + result);
return result;
}
// 4. 匿名内部类:除法操作
public int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("除数不能为0");
}
Operation division = new Operation() {
@Override
public int execute(int x, int y) {
return x / y;
}
};
int result = division.execute(a, b);
logOperation(a + " / " + b + " = " + result);
return result;
}
// 接口用于匿名内部类
private interface Operation {
int execute(int x, int y);
}
private void logOperation(String message) {
history.add(message);
System.out.println("日志: " + message);
}
public void showHistory() {
System.out.println("\n计算历史记录:");
for (String record : history) {
System.out.println(" - " + record);
}
}
public static void main(String[] args) {
Calculator calc = new Calculator();
// 使用成员内部类
Calculator.Addition add = calc.new Addition();
add.calculate(10, 20);
// 使用静态内部类
Calculator.Multiplication mul = new Calculator.Multiplication();
mul.calculate(5, 6);
// 使用局部内部类
calc.subtract(100, 45);
// 使用匿名内部类
calc.divide(30, 5);
// 显示历史记录
calc.showHistory();
}
}
使用建议
- 成员内部类:当内部类需要访问外部类的实例变量时使用
- 静态内部类:当内部类不需要访问外部类实例时使用,更节省内存
- 局部内部类:只在方法内部使用,限定作用范围
- 匿名内部类:用于一次性使用的类,特别是事件处理和回调
这些案例涵盖了Java内部类的主要使用场景,您可以根据实际需求选择合适的内部类类型。
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播