面向对象编程
High Contrast
Dark Mode
Light Mode
Sepia
Forest
1 min read128 words

面向对象编程

类是 Python 的核心——掌握 OOP 才能写出可维护的大型项目。

OOP 核心概念

graph TD OOP[面向对象编程] --> CLASS[类与对象] OOP --> INHERIT[继承与多态] OOP --> ENCAP[封装] OOP --> MAGIC[魔法方法] CLASS --> INIT["__init__ 构造"] CLASS --> METHOD[实例方法] CLASS --> CLS_M[类方法 @classmethod] CLASS --> STATIC[静态方法 @staticmethod] INHERIT --> SINGLE[单继承] INHERIT --> MULTI[多继承 / Mixin] INHERIT --> ABC_T[抽象基类 ABC] MAGIC --> STR["__str__ / __repr__"] MAGIC --> CMP["__eq__ / __lt__"] MAGIC --> CTX["__enter__ / __exit__"] style OOP fill:#e3f2fd,stroke:#1565c0,stroke-width:2px style MAGIC fill:#fff3e0,stroke:#f57c00,stroke-width:2px

类与对象基础

"""
类的定义与使用
"""
class User:
"""用户类"""
# 类变量(所有实例共享)
platform = "MyApp"
_user_count = 0
def __init__(self, name: str, age: int, email: str = ""):
"""构造方法"""
# 实例变量
self.name = name
self.age = age
self.email = email
self._id = User._user_count  # 私有约定(单下划线)
User._user_count += 1
def greet(self) -> str:
"""实例方法"""
return f"你好,我是 {self.name},{self.age} 岁"
@classmethod
def from_dict(cls, data: dict) -> "User":
"""类方法:替代构造函数"""
return cls(
name=data["name"],
age=data["age"],
email=data.get("email", ""),
)
@staticmethod
def validate_age(age: int) -> bool:
"""静态方法:不需要访问实例或类"""
return 0 < age < 150
@property
def display_name(self) -> str:
"""属性:像访问变量一样调用方法"""
return f"{self.name} ({self.email})" if self.email else self.name
def __str__(self) -> str:
return f"User({self.name}, {self.age})"
def __repr__(self) -> str:
return f"User(name={self.name!r}, age={self.age})"
# 使用
user = User("张三", 25, "zhangsan@example.com")
print(user.greet())           # 你好,我是 张三,25 岁
print(user.display_name)      # 张三 (zhangsan@example.com)
print(User.validate_age(25))  # True
# 类方法创建
user2 = User.from_dict({"name": "李四", "age": 30})
print(user2)  # User(李四, 30)
# 类变量
print(User._user_count)       # 2
print(User.platform)          # MyApp

继承与多态

"""
继承与多态
"""
from abc import ABC, abstractmethod
# === 抽象基类 ===
class Shape(ABC):
"""形状抽象基类"""
@abstractmethod
def area(self) -> float:
"""计算面积"""
...
@abstractmethod
def perimeter(self) -> float:
"""计算周长"""
...
def describe(self) -> str:
return f"{self.__class__.__name__}: 面积={self.area():.2f}"
class Circle(Shape):
"""圆"""
def __init__(self, radius: float):
self.radius = radius
def area(self) -> float:
import math
return math.pi * self.radius ** 2
def perimeter(self) -> float:
import math
return 2 * math.pi * self.radius
class Rectangle(Shape):
"""矩形"""
def __init__(self, width: float, height: float):
self.width = width
self.height = height
def area(self) -> float:
return self.width * self.height
def perimeter(self) -> float:
return 2 * (self.width + self.height)
# 多态
shapes: list[Shape] = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(shape.describe())
# Circle: 面积=78.54
# Rectangle: 面积=24.00
# === Mixin 模式 ===
class JsonMixin:
"""JSON 序列化 Mixin"""
def to_json(self) -> str:
import json
return json.dumps(self.__dict__, ensure_ascii=False)
@classmethod
def from_json(cls, json_str: str):
import json
return cls(**json.loads(json_str))
class LogMixin:
"""日志 Mixin"""
def log(self, message: str):
print(f"[{self.__class__.__name__}] {message}")
class Product(JsonMixin, LogMixin):
"""产品(继承多个 Mixin)"""
def __init__(self, name: str, price: float):
self.name = name
self.price = price
product = Product("Python 书", 59.9)
print(product.to_json())   # {"name": "Python 书", "price": 59.9}
product.log("创建成功")     # [Product] 创建成功

魔法方法

"""
常用魔法方法(Dunder methods)
"""
class Money:
"""金额类——演示常用魔法方法"""
def __init__(self, amount: float, currency: str = "CNY"):
self.amount = amount
self.currency = currency
# --- 字符串表示 ---
def __str__(self) -> str:
"""用户友好的字符串"""
symbols = {"CNY": "¥", "USD": "$", "EUR": "€"}
symbol = symbols.get(self.currency, self.currency)
return f"{symbol}{self.amount:.2f}"
def __repr__(self) -> str:
"""开发者友好的字符串"""
return f"Money({self.amount}, {self.currency!r})"
# --- 比较运算 ---
def __eq__(self, other: "Money") -> bool:
return self.amount == other.amount and self.currency == other.currency
def __lt__(self, other: "Money") -> bool:
if self.currency != other.currency:
raise ValueError("不同货币不能比较")
return self.amount < other.amount
def __le__(self, other: "Money") -> bool:
return self == other or self < other
# --- 算术运算 ---
def __add__(self, other: "Money") -> "Money":
if self.currency != other.currency:
raise ValueError("不同货币不能相加")
return Money(self.amount + other.amount, self.currency)
def __mul__(self, factor: float) -> "Money":
return Money(self.amount * factor, self.currency)
# --- 容器协议 ---
def __bool__(self) -> bool:
"""布尔值:金额非零为 True"""
return self.amount != 0
def __hash__(self) -> int:
"""支持作为字典键和集合元素"""
return hash((self.amount, self.currency))
# 使用
price = Money(99.9)
tax = Money(7.99)
total = price + tax
print(f"价格: {price}")      # 价格: ¥99.90
print(f"总计: {total}")      # 总计: ¥107.89
print(f"双倍: {price * 2}")  # 双倍: ¥199.80
print(price < total)          # True
print(repr(price))            # Money(99.9, 'CNY')

dataclass

"""
dataclass:自动生成样板代码
"""
from dataclasses import dataclass, field
@dataclass
class Config:
"""配置类"""
host: str = "localhost"
port: int = 8080
debug: bool = False
tags: list[str] = field(default_factory=list)
def url(self) -> str:
return f"http://{self.host}:{self.port}"
# 自动生成 __init__, __repr__, __eq__
config = Config(port=3000, debug=True)
print(config)       # Config(host='localhost', port=3000, debug=True, tags=[])
print(config.url()) # http://localhost:3000
# 比较
config2 = Config(port=3000, debug=True)
print(config == config2)  # True
# --- 不可变 dataclass ---
@dataclass(frozen=True)
class Point:
x: float
y: float
def distance(self, other: "Point") -> float:
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
p1 = Point(0, 0)
p2 = Point(3, 4)
print(p1.distance(p2))  # 5.0
# p1.x = 10  # FrozenInstanceError!
# --- 排序 ---
@dataclass(order=True)
class Student:
sort_index: float = field(init=False, repr=False)
name: str
grade: float
def __post_init__(self):
self.sort_index = self.grade
students = [Student("张三", 85), Student("李四", 92), Student("王五", 78)]
students.sort(reverse=True)
for s in students:
print(f"{s.name}: {s.grade}")
# 李四: 92, 张三: 85, 王五: 78

本章小结

概念 关键点
类与对象 __init__, 实例变量, 类变量
方法类型 实例方法, @classmethod, @staticmethod, @property
继承 ABC, 多继承, Mixin 模式
魔法方法 __str__, __eq__, __add__, __hash__
dataclass 自动 init/repr/eq, frozen, order

下一章:函数式编程——高阶函数、装饰器、闭包。