本文目录导读:

我来为你展示一个完整的静态内部类案例,包含实际应用场景。
基础案例:经典的单例模式
public class Singleton {
// 私有构造函数
private Singleton() {
System.out.println("Singleton 实例被创建");
}
// 静态内部类(饿汉式单例的优化版)
private static class SingletonHolder {
// 静态常量,类加载时初始化
private static final Singleton INSTANCE = new Singleton();
}
// 对外提供获取实例的方法
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
public void showMessage() {
System.out.println("Hello from Singleton!");
}
public static void main(String[] args) {
// 测试单例模式
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println("s1 == s2 ? " + (s1 == s2)); // true
s1.showMessage();
}
}
实际应用:实现链表的 Node 类
public class LinkedList {
// 静态内部类 - 链表节点
private static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
private Node head;
private int size;
public LinkedList() {
head = null;
size = 0;
}
// 添加元素
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
size++;
}
// 移除元素
public boolean remove(int data) {
if (head == null) return false;
if (head.data == data) {
head = head.next;
size--;
return true;
}
Node current = head;
while (current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
size--;
return true;
}
current = current.next;
}
return false;
}
// 遍历打印
public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public int getSize() {
return size;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println("链表元素:");
list.display(); // 10 20 30 40
list.remove(20);
System.out.println("删除20后:");
list.display(); // 10 30 40
System.out.println("链表大小: " + list.getSize()); // 3
}
}
实际应用:计算器中的运算单元
import java.util.ArrayList;
import java.util.List;
public class Calculator {
// 静态内部类 - 运算结果
public static class Result {
private final double value;
private final String expression;
public Result(double value, String expression) {
this.value = value;
this.expression = expression;
}
public double getValue() {
return value;
}
public String getExpression() {
return expression;
}
@Override
public String toString() {
return expression + " = " + value;
}
}
// 静态内部类 - 历史记录
private static class History {
private final List<Result> records = new ArrayList<>();
public void add(Result result) {
records.add(result);
}
public void clear() {
records.clear();
}
public List<Result> getRecords() {
return new ArrayList<>(records);
}
public Result getLast() {
if (records.isEmpty()) {
return null;
}
return records.get(records.size() - 1);
}
public int size() {
return records.size();
}
}
private final History history;
public Calculator() {
this.history = new History();
}
// 加法
public Result add(double a, double b) {
Result result = new Result(a + b, a + " + " + b);
history.add(result);
return result;
}
// 减法
public Result subtract(double a, double b) {
Result result = new Result(a - b, a + " - " + b);
history.add(result);
return result;
}
// 乘法
public Result multiply(double a, double b) {
Result result = new Result(a * b, a + " × " + b);
history.add(result);
return result;
}
// 除法
public Result divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("除数不能为0");
}
Result result = new Result(a / b, a + " ÷ " + b);
history.add(result);
return result;
}
// 显示历史记录
public void showHistory() {
System.out.println("=== 计算历史 ===");
for (Result r : history.getRecords()) {
System.out.println(r);
}
System.out.println("===============");
}
public static void main(String[] args) {
Calculator calc = new Calculator();
// 使用静态内部类 Result
Calculator.Result r1 = calc.add(10, 5);
Calculator.Result r2 = calc.subtract(10, 3);
Calculator.Result r3 = calc.multiply(4, 6);
Calculator.Result r4 = calc.divide(20, 4);
System.out.println("最新结果: " + r1);
System.out.println("所有结果: " + r2);
System.out.println("乘法结果: " + r3);
System.out.println("除法结果: " + r4);
// 显示计算历史
calc.showHistory();
// 直接访问静态内部类(不需要外部类实例)
Calculator.Result standalone = new Calculator.Result(42, "6 × 7");
System.out.println("独立结果: " + standalone);
}
}
实际应用:构建者模式
public class Person {
// 必填字段
private final String name;
private final int age;
// 可选字段
private final String email;
private final String phone;
private final String address;
// 私有构造函数,接收 Builder
private Person(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.email = builder.email;
this.phone = builder.phone;
this.address = builder.address;
}
// 静态内部类 - Builder
public static class Builder {
// 必填字段
private final String name;
private final int age;
// 可选字段(带默认值)
private String email = "";
private String phone = "";
private String address = "";
public Builder(String name, int age) {
this.name = name;
this.age = age;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Person build() {
return new Person(this);
}
}
// getter 方法
public String getName() { return name; }
public int getAge() { return age; }
public String getEmail() { return email; }
public String getPhone() { return phone; }
public String getAddress() { return address; }
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
public static void main(String[] args) {
// 使用 Builder 创建对象
Person person1 = new Person.Builder("张三", 25)
.email("zhangsan@example.com")
.phone("138-0000-0000")
.build();
Person person2 = new Person.Builder("李四", 30)
.email("lisi@example.com")
.address("北京市海淀区")
.build();
Person person3 = new Person.Builder("王五", 28).build();
System.out.println(person1);
System.out.println(person2);
System.out.println(person3);
}
}
实际应用:数据结构的 Pair 类
public class PairExample {
// 静态内部类 - 通用的键值对
public static class Pair<K, V> {
private final K key;
private final V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
@Override
public String toString() {
return "(" + key + ", " + value + ")";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) obj;
return key.equals(pair.key) && value.equals(pair.value);
}
@Override
public int hashCode() {
return key.hashCode() * 31 + value.hashCode();
}
}
public static void main(String[] args) {
// 创建各种类型的 Pair
Pair<String, Integer> pair1 = new Pair<>("年龄", 25);
Pair<String, String> pair2 = new Pair<>("城市", "上海");
Pair<Integer, String> pair3 = new Pair<>(1, "第一名");
System.out.println("Pair1: " + pair1);
System.out.println("Pair2: " + pair2);
System.out.println("Pair3: " + pair3);
// 创建集合存放多个 Pair
List<Pair<String, Integer>> scores = new ArrayList<>();
scores.add(new Pair<>("张三", 90));
scores.add(new Pair<>("李四", 85));
scores.add(new Pair<>("王五", 92));
System.out.println("\n学生成绩: ");
for (Pair<String, Integer> score : scores) {
System.out.println(score.getKey() + ": " + score.getValue() + "分");
}
}
}
静态内部类的主要优点
- 不用依赖外部类的实例:可以独立创建使用
- 更好的封装性:将相关的类组织在一起
- 节省内存:相比非静态内部类,不会隐式持有外部类的引用
- 实现单例模式的安全方式:如案例1所示
- 代码组织更清晰:Builder 模式
- 🔹 单例模式:使用静态内部类实现懒加载且线程安全的单例
- 🔹 数据结构辅助类:如链表的 Node 节点
- 🔹 构建者模式:Builder 内部类
- 🔹 工具类分组:将相关的辅助类放在一起
- 🔹 公共数据结构:如 Pair、Result 等通用数据结构
静态内部类是 Java 中非常实用的特性,合理使用可以让代码更加清晰、高效。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播