里氏替换原则 logo

里氏替换原则

Organization
microwind
里氏替换原则

子类型必须能够替换其基类型而不改变程序的正确性。所有引用基类的地方必须能透明地使用其子类对象。

Overview

Publishermicrowind
Repositoryai-skills
Skill name里氏替换原则
Stars
93
Forks
25
Bundled files
2
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 里氏替换原则 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/design-principles/liskov-substitution-principle
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/design-principles/liskov-substitution-principle .claude/skills/microwind-45
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 里氏替换原则 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 里氏替换原则 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 里氏替换原则 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

里氏替换原则 (Liskov Substitution Principle, LSP)

概述

里氏替换原则由 Barbara Liskov 于1987年提出,是 SOLID 原则中最容易被忽视但最关键的一条:子类型必须能替换其父类型。

核心思想:

  • 子类可以扩展父类功能,但不能改变父类原有的行为
  • 任何使用父类的地方,换成子类必须仍然正确
  • 子类不应该加强前置条件或削弱后置条件
  • 违反 LSP 通常意味着继承关系设计错误

契约规则:

  • 前置条件:子类不能比父类更严格
  • 后置条件:子类不能比父类更宽松
  • 不变量:子类必须维护父类的所有不变量

何时使用

始终使用:

  • 使用继承构建类层次结构时
  • 设计接口和抽象类时
  • 泛型/模板编程中约束类型参数
  • 多态替换场景

触发短语:

  • "子类重写了父类方法但行为完全不同"
  • "用子类替换父类后系统出现异常"
  • "需要通过 instanceof 判断具体类型再处理"
  • "子类抛出了父类不会抛出的异常"

经典案例:正方形与长方形

java
// ❌ 违反 LSP:正方形继承长方形
public class Rectangle {
    protected int width;
    protected int height;

    public void setWidth(int width) { this.width = width; }
    public void setHeight(int height) { this.height = height; }
    public int getArea() { return width * height; }
}

public class Square extends Rectangle {
    @Override
    public void setWidth(int width) {
        this.width = width;
        this.height = width;  // 强制宽高相等,改变了父类行为
    }

    @Override
    public void setHeight(int height) {
        this.width = height;
        this.height = height;  // 强制宽高相等
    }
}

// 使用父类引用时出现问题
public void resize(Rectangle rect) {
    rect.setWidth(5);
    rect.setHeight(10);
    assert rect.getArea() == 50;  // Square 时失败!面积是 100
}
java
// ✅ 正确设计:使用共同抽象
public interface Shape {
    int getArea();
}

public class Rectangle implements Shape {
    private final int width;
    private final int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public int getArea() { return width * height; }
}

public class Square implements Shape {
    private final int side;

    public Square(int side) { this.side = side; }

    @Override
    public int getArea() { return side * side; }
}

经典案例:鸟与企鹅

java
// ❌ 违反 LSP:企鹅是鸟,但不会飞
public class Bird {
    public void fly() {
        System.out.println("飞行中...");
    }
}

public class Penguin extends Bird {
    @Override
    public void fly() {
        throw new UnsupportedOperationException("企鹅不会飞");
        // 子类抛出了父类不会抛出的异常
    }
}

public void makeBirdFly(Bird bird) {
    bird.fly();  // Penguin 时抛异常!
}
java
// ✅ 正确设计:分离飞行能力
public abstract class Bird {
    public abstract String getName();
}

public interface Flyable {
    void fly();
}

public class Sparrow extends Bird implements Flyable {
    @Override
    public String getName() { return "麻雀"; }

    @Override
    public void fly() { System.out.println("麻雀飞行中"); }
}

public class Penguin extends Bird {
    @Override
    public String getName() { return "企鹅"; }
    // 企鹅不实现 Flyable,不会被要求飞行
}

违反 LSP 的常见表现

1. 子类抛出意外异常

java
// ❌ 父类不抛异常,子类抛异常
public class FileReader {
    public String read(String path) {
        return "文件内容";
    }
}

public class EncryptedFileReader extends FileReader {
    @Override
    public String read(String path) {
        if (!hasPermission()) {
            throw new SecurityException("无权限");  // 违反 LSP
        }
        return decrypt(super.read(path));
    }
}

2. 子类忽略父类方法

java
// ❌ 空实现 = 违反 LSP
public interface Collection<T> {
    void add(T item);
    void remove(T item);
    int size();
}

public class ReadOnlyList<T> implements Collection<T> {
    @Override
    public void add(T item) {
        // 什么都不做,静默忽略
    }

    @Override
    public void remove(T item) {
        throw new UnsupportedOperationException();  // 或者抛异常
    }
}

3. 需要类型检查才能工作

java
// ❌ 代码中出现 instanceof 判断 = LSP 违反信号
public void processShape(Shape shape) {
    if (shape instanceof Circle) {
        // Circle 特殊处理
    } else if (shape instanceof Rectangle) {
        // Rectangle 特殊处理
    }
    // 每新增一种 Shape 都要修改这里
}

// ✅ 利用多态,无需类型判断
public void processShape(Shape shape) {
    shape.draw();  // 每种 Shape 自己实现 draw
}

代码示例 - 完整实现

Python

python
from abc import ABC, abstractmethod

# ❌ 违反 LSP
class Account:
    def __init__(self, balance: float):
        self.balance = balance

    def withdraw(self, amount: float):
        if amount > self.balance:
            raise ValueError("余额不足")
        self.balance -= amount

class FixedDepositAccount(Account):
    """定期存款账户"""
    def withdraw(self, amount: float):
        raise RuntimeError("定期账户不允许提前支取")
        # 违反了父类的契约

# ✅ 遵循 LSP
class Account(ABC):
    def __init__(self, balance: float):
        self.balance = balance

    @abstractmethod
    def can_withdraw(self, amount: float) -> bool:
        pass

    def withdraw(self, amount: float):
        if not self.can_withdraw(amount):
            raise ValueError("不满足提取条件")
        self.balance -= amount

class SavingsAccount(Account):
    def can_withdraw(self, amount: float) -> bool:
        return amount <= self.balance

class FixedDepositAccount(Account):
    def __init__(self, balance: float, maturity_date):
        super().__init__(balance)
        self.maturity_date = maturity_date

    def can_withdraw(self, amount: float) -> bool:
        from datetime import date
        return date.today() >= self.maturity_date and amount <= self.balance

TypeScript

typescript
// ❌ 违反 LSP
interface Logger {
    log(message: string): void;
    getHistory(): string[];
}

class ConsoleLogger implements Logger {
    private history: string[] = [];

    log(message: string): void {
        console.log(message);
        this.history.push(message);
    }

    getHistory(): string[] {
        return [...this.history];
    }
}

class ExternalLogger implements Logger {
    log(message: string): void {
        // 发送到外部服务
        fetch('/api/log', { method: 'POST', body: message });
    }

    getHistory(): string[] {
        return [];  // 无法获取历史,返回空数组,违反语义契约
    }
}

// ✅ 遵循 LSP:分离接口
interface Logger {
    log(message: string): void;
}

interface HistoryLogger extends Logger {
    getHistory(): string[];
}

class ConsoleLogger implements HistoryLogger {
    private history: string[] = [];

    log(message: string): void {
        console.log(message);
        this.history.push(message);
    }

    getHistory(): string[] {
        return [...this.history];
    }
}

class ExternalLogger implements Logger {
    log(message: string): void {
        fetch('/api/log', { method: 'POST', body: message });
    }
    // 不需要实现 getHistory
}

优缺点分析

✅ 优点

  1. 提高可替换性 - 子类可以安全地替换父类
  2. 增强多态性 - 多态代码更可靠、更可预测
  3. 减少条件判断 - 不需要 instanceof 检查
  4. 促进正确的继承设计 - 迫使思考"is-a"关系的本质
  5. 提升可测试性 - mock 对象能真正替代真实对象

❌ 缺点

  1. 设计难度高 - 需要仔细思考契约
  2. 可能导致类层次重构 - 发现违反时需要重新设计
  3. 过度约束 - 某些场景下完美遵循很困难

最佳实践

1. 用"行为一致性"验证继承

问自己:
□ 子类的每个方法是否都满足父类方法的契约?
□ 子类是否维护了父类的所有不变量?
□ 用子类替换父类后,所有测试是否仍然通过?
□ 调用方是否需要知道具体子类类型才能正确工作?

2. 优先使用组合而非继承

java
// 当 "is-a" 关系不成立时,用组合代替继承
public class FixedDeposit {
    private final Account account;  // 组合而非继承
    private final LocalDate maturityDate;

    public void withdrawOnMaturity() {
        if (LocalDate.now().isBefore(maturityDate)) {
            throw new IllegalStateException("未到期");
        }
        account.withdraw(account.getBalance());
    }
}

3. 基于契约编程

java
// 明确定义方法的前置条件和后置条件
public abstract class Sorter {
    /**
     * 前置条件: list != null
     * 后置条件: 返回的列表包含原列表所有元素,且有序
     */
    public abstract <T extends Comparable<T>> List<T> sort(List<T> list);
}

4. 编写父类测试套件验证子类

java
// 父类的测试应该对所有子类都通过
public abstract class AccountTest<T extends Account> {
    protected abstract T createAccount(double balance);

    @Test
    public void withdrawShouldReduceBalance() {
        T account = createAccount(100);
        account.withdraw(30);
        assertEquals(70, account.getBalance(), 0.01);
    }

    @Test
    public void withdrawExceedingBalanceShouldFail() {
        T account = createAccount(100);
        assertThrows(Exception.class, () -> account.withdraw(200));
    }
}

// 每个子类继承这套测试
public class SavingsAccountTest extends AccountTest<SavingsAccount> {
    @Override
    protected SavingsAccount createAccount(double balance) {
        return new SavingsAccount(balance);
    }
}

与其他原则的关系

原则关系
SRPSRP 保证类职责单一,LSP 保证替换安全
OCPLSP 是 OCP 的基础,违反 LSP 常导致违反 OCP
ISPISP 拆分接口,避免子类被迫实现不需要的方法
DIPDIP 依赖抽象,LSP 保证抽象的子类可安全替换

总结

LSP 核心:

  • 子类型必须能完全替换父类型
  • 不改变前置条件、后置条件和不变量
  • 违反 LSP 的信号:instanceof、空实现、意外异常

检查方法:

  • 父类的测试套件对子类全部通过
  • 不需要 instanceof 判断具体类型
  • 子类不抛出父类未声明的异常
  • 子类不忽略父类方法的语义

修复违反:

  • 重新审视"is-a"关系
  • 使用组合替代继承
  • 提取更细粒度的接口
  • 引入抽象层分离行为差异

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 里氏替换原则 AI skill do?

子类型必须能够替换其基类型而不改变程序的正确性。所有引用基类的地方必须能透明地使用其子类对象。

Why use 里氏替换原则 on TypingMind?

Because you install it once and use it with any model. 里氏替换原则 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 里氏替换原则 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/design-principles/liskov-substitution-principle. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 里氏替换原则?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 里氏替换原则?

As many as you like. As long as a model supports skills, you can use 里氏替换原则 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 里氏替换原则 AI skill free?

Yes. It is published on GitHub by microwind under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇