面向对象编程(Object-Oriented Programming, OOP)是 Python 的核心范式之一,通过**类(Class)和对象(Object)**组织代码,提供封装、继承、多态等特性。以下从基础语法到高级特性全面解析,并附示例代码。
# 定义一个类
class Dog:
# 类属性(所有实例共享)
species = "Canis familiaris"
# 初始化方法(构造函数)
def __init__(self, name, age):
self.name = name # 实例属性
self.age = age
# 实例方法
def bark(self):
print(f"{self.name} says: Woof!")
# 创建对象
my_dog = Dog("Buddy", 3)
print(my_dog.name) # 输出: Buddy
print(my_dog.species) # 输出: Canis familiaris
my_dog.bark() # 输出: Buddy says: Woof!
通过访问修饰符控制属性和方法的可见性(Python 通过命名约定实现)。
__
前缀(实际通过名称修饰实现,如 _ClassName__member
)。class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner # 公有属性
self.__balance = balance # 私有属性
# 公有方法
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited ${amount}. New balance: ${self.__balance}")
else:
print("Invalid amount.")
def get_balance(self):
return self.__balance
# 使用示例
account = BankAccount("Alice", 100)
account.deposit(50) # 输出: Deposited $50. New balance: $150
# print(account.__balance) # 报错: AttributeError
print(account.get_balance()) # 输出: 150
子类继承父类的属性和方法,并可扩展或重写父类行为。
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
class Cat(Animal):
# 重写父类方法
def speak(self):
print(f"{self.name} says: Meow!")
class Dog(Animal):
def speak(self):
print(f"{self.name} says: Woof!")
# 多态示例
animals = [Cat("Whiskers"), Dog("Buddy")]
for animal in animals:
animal.speak()
# 输出:
# Whiskers says: Meow!
# Buddy says: Woof!
通过抽象基类(Abstract Base Class, ABC)强制子类实现特定方法。
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
def perimeter(self):
return 2 * 3.14 * self.radius
# 实例化
circle = Circle(5)
print(circle.area()) # 输出: 78.5
print(circle.perimeter()) # 输出: 31.4
cls
参数。class Person:
total_people = 0
def __init__(self, name):
self.name = name
Person.total_people += 1
@classmethod
def get_total(cls):
return cls.total_people
@staticmethod
def is_adult(age):
return age >= 18
# 使用示例
p1 = Person("Alice")
p2 = Person("Bob")
print(Person.get_total()) # 输出: 2
print(Person.is_adult(20)) # 输出: True
控制属性访问逻辑,实现 getter/setter。
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
if value < -273.15:
raise ValueError("Temperature below absolute zero is invalid")
self._celsius = value
@property
def fahrenheit(self):
return (self._celsius * 9/5) + 32
# 使用示例
temp = Temperature(25)
print(temp.fahrenheit) # 输出: 77.0
temp.celsius = 30 # 合法
# temp.celsius = -300 # 抛出 ValueError
通过特殊方法实现对象行为定制(如运算符重载、字符串表示)。
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# 运算符重载(+)
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
# 字符串表示(print() 时调用)
def __str__(self):
return f"Vector({self.x}, {self.y})"
# 正式字符串表示(解释器显示)
def __repr__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 3)
v2 = Vector(5, 7)
print(v1 + v2) # 输出: Vector(7, 10)
通过对象组合实现复杂功能(非继承)。
class Engine:
def start(self):
print("Engine started")
class Car:
def __init__(self):
self.engine = Engine() # 组合关系
def start(self):
self.engine.start()
print("Car is ready to drive")
car = Car()
car.start()
# 输出:
# Engine started
# Car is ready to drive
Mixin类是一种多继承的模式,它允许将功能添加到类中,而不需要修改类的继承关系。Mixin类通常用于向多个类添加可重用的功能,而不是作为独立的类使用。以下是Mixin类的一些特点:
class JsonMixin:
def to_json(self):
import json
return json.dumps(self.__dict__)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class JsonPerson(Person, JsonMixin):
pass
person = JsonPerson('Alice', 30)
print(person.to_json()) # 输出: {"name": "Alice", "age": 30}
在这个例子中,JsonMixin
提供了一个 to_json
方法,可以由任何继承它的类使用。JsonPerson
通过多继承同时继承了 Person
的属性和方法以及 JsonMixin
的 to_json
方法。
使用Mixin类时应该注意以下几点:
Mixin类在Python社区中被广泛使用,尤其是在Django等Web框架中,它们提供了一种灵活的方式来扩展类的功能。
通过合理使用 OOP 特性,可以构建高内聚、低耦合的模块化代码。