# 错误：记录不能继承类

- **ID:** `java/records-cannot-extend-class`
- **领域:** java
- **类别:** compilation_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

Java 记录隐式继承 java.lang.Record，不能继承其他任何类，因为它们是 final 的且超类是预定的。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 16 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## 解决方案

1. ```
   将记录转换为继承所需类的普通类，并手动实现 equals、hashCode、toString 和构造函数。示例：`public class MyClass extends BaseClass { private final int x; public MyClass(int x) { this.x = x; } // 加上 equals/hashCode/toString }`
   ```
2. ```
   使用组合而非继承：让记录包含基类类型的字段。示例：`public record MyRecord(BaseClass base, int x) { }`
   ```
3. ```
   如果需要基类功能，创建一个基类实现的接口，然后让记录实现该接口。
   ```

## 无效尝试

- **Remove the 'record' keyword and make it a regular class that extends the desired class.** — This loses all record features (canonical constructor, equals/hashCode/toString), requiring manual implementation and breaking serialization behavior. (70% 失败率)
- **Use 'implements' instead of 'extends' on the record.** — Records can implement interfaces but not extend classes; the error message is about extending classes, not interfaces. (90% 失败率)
- **Make the record abstract to allow extending.** — Records cannot be abstract; the compiler will reject with 'illegal combination of modifiers: abstract and final'. (100% 失败率)
