Python 的面向对象编程(OOP)通过类(Class)和对象(Object)实现代码结构化,支持封装、继承和多态三大特性。以下是系统化指南:
一、类与对象基础
1. 定义类
python
class Dog:
# 类属性(所有实例共享)
species = "Canis familiaris"
# 构造方法(__init__是魔法方法)
def __init__(self, name, age):
self.name = name # 实例属性
self.age = age
# 实例方法
def bark(self):
return f"{self.name} says: Woof!"
# 创建对象
my_dog = Dog("Buddy", 3)
print(my_dog.species) # 输出: Canis familiaris
2. 特殊方法(Magic Methods)
python
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
# 计算面积
def area(self):
return self.width * self.height
# 字符串表示(__str__用于用户友好显示)
def __str__(self):
return f"Rectangle({self.width}x{self.height})"
# 运算符重载(实现加法)
def __add__(self, other):
return Rectangle(
self.width + other.width,
self.height + other.height
)
rect1 = Rectangle(2, 3)
rect2 = Rectangle(4, 5)
print(rect1 + rect2) # 输出: Rectangle(6x8)
二、继承与方法重写
1. 单继承
python
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("子类必须实现此方法")
class Dog(Animal):
def speak(self):
return f"{self.name} says: Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says: Meow!"
animals = [Dog("Buddy"), Cat("Whiskers")]
for animal in animals:
print(animal.speak())
2. 多重继承
python
class Flyer:
def fly(self):
return "Flying!"
class Swimmer:
def swim(self):
return "Swimming!"
class Duck(Flyer, Swimmer):
def __init__(self, name):
self.name = name
duck = Duck("Donald")
print(duck.fly()) # 输出: Flying!
print(duck.swim()) # 输出: Swimming!
3. super() 函数
python
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
class ColoredRectangle(Rectangle):
def __init__(self, width, height, color):
super().__init__(width, height) # 调用父类构造方法
self.color = color
三、多态与鸭子类型
1. 运行时多态
python
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
def print_area(shape):
print(shape.area())
print_area(Circle(5)) # 输出: 78.5
print_area(Square(4)) # 输出: 16
2. 鸭子类型(Duck Typing)
python
class Duck:
def quack(self):
print("Quack!")
class FakeDuck:
def quack(self):
print("Silent quack")
def make_quack(duck):
duck.quack()
make_quack(Duck()) # 输出: Quack!
make_quack(FakeDuck()) # 输出: Silent quack
四、封装与访问控制
1. 属性控制
python
class BankAccount:
def __init__(self, balance=0):
self.__balance = balance # 双下划线前缀实现名称修饰
@property
def balance(self):
return self.__balance
@balance.setter
def balance(self, value):
if value < 0:
raise ValueError("余额不能为负")
self.__balance = value
account = BankAccount(100)
account.balance = 200 # 允许修改
# account.balance = -50 # 抛出 ValueError
2. 描述符协议
python
class NonNegative:
def __set_name__(self, owner, name):
self.name = name
def __get__(self, instance, owner):
return instance.__dict__[self.name]
def __set__(self, instance, value):
if value < 0:
raise ValueError("值不能为负")
instance.__dict__[self.name] = value
class Product:
price = NonNegative()
stock = NonNegative()
def __init__(self, price, stock):
self.price = price
self.stock = stock
product = Product(19.99, 100)
# product.price = -5 # 抛出 ValueError
五、高级特性
1. 元类(Metaclass)
python
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class Database(metaclass=Singleton):
pass
db1 = Database()
db2 = Database()
print(db1 is db2) # 输出: True
2. 抽象基类(ABC)
python
from abc import ABC, abstractmethod
class PaymentGateway(ABC):
@abstractmethod
def process_payment(self, amount):
pass
class PayPal(PaymentGateway):
def process_payment(self, amount):
print(f"PayPal 处理支付: ${amount}")
# class FakeGateway(PaymentGateway): pass # 实例化会报错
六、设计模式应用
1. 工厂模式
python
class ShapeFactory:
@staticmethod
def create_shape(shape_type, **kwargs):
shapes = {
'circle': Circle,
'square': Square
}
return shapes[shape_type](**kwargs)
circle = ShapeFactory.create_shape('circle', radius=5)
2. 单例模式
python
class Database:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if not self._initialized:
self.connect()
self._initialized = True
def connect(self):
print("建立数据库连接")
七、最佳实践
-
组合优于继承:优先使用对象组合而非类继承
-
显式优于隐式:避免过度依赖魔术方法
-
保持简单:单个类职责不超过7个方法(参考SRP原则)
-
文档字符串:为每个类和方法编写docstring
-
类型注解 (Python 3.5+):
pythonclass Vector: def __init__(self, x: float, y: float): self.x = x self.y = y
掌握这些面向对象技术后,可进一步探索设计模式(如MVC、观察者模式)和框架开发(如Django的类视图)。建议通过实际项目(如开发电商系统、游戏引擎)深化理解。