一、方法类型总览
python中的方法主要分为以下几种:
方法分类: ┌───────────────┐ │ 实例方法 │ ← 操作实例属性,第一个参数self ├───────────────┤ │ 类方法 │ ← 操作类属性,@classmethod装饰,第一个参数cls ├───────────────┤ │ 静态方法 │ ← 不操作类或实例属性,@staticmethod装饰 ├───────────────┤ │ 抽象方法 │ ← 必须由子类实现,@abstractmethod装饰 ├───────────────┤ │ 魔术方法 │ ← 双下划线开头结尾,如__init__ └───────────────┘
二、各类方法详解
1. 实例方法 (instance method)
特点:
- 默认类型的方法
- 第一个参数必须是
self
,指向实例本身 - 可以访问和修改实例属性
- 可以访问类属性
class myclass: class_attr = "类属性" def __init__(self, value): self.instance_attr = value # 实例属性 def instance_method(self): return f"实例属性: {self.instance_attr}, 类属性: {self.class_attr}" obj = myclass("实例值") print(obj.instance_method()) # 实例属性: 实例值, 类属性: 类属性
2. 类方法 (class method)
特点:
- 使用
@classmethod
装饰器 - 第一个参数必须是
cls
,指向类本身 - 可以访问和修改类属性
- 不能访问实例属性
- 常用于创建工厂方法
class pizza: base_price = 10 # 基础价格 def __init__(self, toppings): self.toppings = toppings @classmethod def margherita(cls): return cls(["番茄", "芝士"]) # 创建特定类型的披萨 @classmethod def pepperoni(cls): return cls(["番茄", "芝士", "意大利辣香肠"]) @classmethod def update_base_price(cls, new_price): cls.base_price = new_price # 修改类属性 # 使用类方法创建对象 margherita = pizza.margherita() pepperoni = pizza.pepperoni() print(margherita.toppings) # ['番茄', '芝士'] print(pepperoni.toppings) # ['番茄', '芝士', '意大利辣香肠'] # 修改类属性 pizza.update_base_price(12) print(pizza.base_price) # 12
3. 静态方法 (static method)
特点:
- 使用
@staticmethod
装饰器 - 不需要
self
或cls
参数 - 不能访问类或实例属性
- 与普通函数类似,但逻辑上属于类
- 常用于工具函数
class mathutils: @staticmethod def add(a, b): return a + b @staticmethod def circle_area(radius): return 3.14159 * radius ** 2 # 使用静态方法 print(mathutils.add(5, 3)) # 8 print(mathutils.circle_area(2)) # 12.56636 # 也可以通过实例调用 utils = mathutils() print(utils.add(10, 20)) # 30
4. 抽象方法 (abstract method)
特点:
- 使用
@abstractmethod
装饰器 - 必须从
abc.abc
继承 - 只定义接口不实现,子类必须实现
- 用于定义抽象基类(abc)
from abc import abc, abstractmethod class animal(abc): @abstractmethod def make_sound(self): pass @abstractmethod def move(self): pass class dog(animal): def make_sound(self): return "汪汪!" def move(self): return "跑动" # animal = animal() # 报错,不能实例化抽象类 dog = dog() print(dog.make_sound()) # 汪汪! print(dog.move()) # 跑动
5. 魔术方法 (magic method)
特点:
- 双下划线开头和结尾(
__method__
) - python自动调用,用于实现特殊行为
- 如
__init__
(初始化)、__str__
(字符串表示)等
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) def __str__(self): return f"vector({self.x}, {self.y})" def __len__(self): """返回向量长度(欧几里得距离)""" return int((self.x**2 + self.y**2)**0.5) v1 = vector(3, 4) v2 = vector(5, 6) v3 = v1 + v2 # 调用__add__ print(v3) # vector(8, 10) (调用__str__) print(len(v1)) # 5 (调用__len__)
三、方法修饰符对比表
特性 | 实例方法 | 类方法 | 静态方法 | 抽象方法 |
---|---|---|---|---|
装饰器 | 无 | @classmethod | @staticmethod | @abstractmethod |
第一个参数 | self(实例) | cls(类) | 无 | self或cls |
访问实例属性 | 可以 | 不可以 | 不可以 | 可以(需子类实现) |
访问类属性 | 可以 | 可以 | 不可以 | 可以(需子类实现) |
调用方式 | 对象.方法() | 类.方法()或对象.方法() | 类.方法()或对象.方法() | 子类必须实现 |
主要用途 | 操作实例状态 | 操作类状态或工厂方法 | 工具函数 | 定义接口规范 |
四、综合案例:电商商品系统
让我们通过一个电商商品系统的案例来综合运用各种方法类型:
from abc import abc, abstractmethod from datetime import datetime class product(abc): """商品抽象基类""" tax_rate = 0.1 # 类属性: 税率 def __init__(self, name, price, quantity): self.name = name self.price = price self.quantity = quantity self.__id = self.__generate_id() # 私有属性 def __generate_id(self): """私有方法: 生成商品id""" timestamp = int(datetime.now().timestamp()) return f"prod-{timestamp}" @property def id(self): """只读属性: 商品id""" return self.__id @abstractmethod def display_info(self): """抽象方法: 显示商品信息""" pass @classmethod def update_tax_rate(cls, new_rate): """类方法: 更新税率""" cls.tax_rate = new_rate @staticmethod def calculate_discount(price, discount): """静态方法: 计算折扣价""" return price * (1 - discount) def sell(self, quantity): """实例方法: 销售商品""" if quantity <= self.quantity: self.quantity -= quantity total = quantity * self.price * (1 + self.tax_rate) return f"已售出 {quantity} 件 {self.name}, 总价: {total:.2f}" return "库存不足" class book(product): """具体商品类: 图书""" def __init__(self, name, price, quantity, author): super().__init__(name, price, quantity) self.author = author def display_info(self): """实现抽象方法""" return (f"图书: {self.name}\n" f"作者: {self.author}\n" f"价格: ¥{self.price:.2f}\n" f"库存: {self.quantity}件\n" f"含税价: ¥{self.price * (1 + self.tax_rate):.2f}") class electronics(product): """具体商品类: 电子产品""" def __init__(self, name, price, quantity, warranty): super().__init__(name, price, quantity) self.warranty = warranty # 保修期(月) def display_info(self): """实现抽象方法""" return (f"电子产品: {self.name}\n" f"保修: {self.warranty}个月\n" f"价格: ¥{self.price:.2f}\n" f"库存: {self.quantity}件\n" f"含税价: ¥{self.price * (1 + self.tax_rate):.2f}") # 使用示例 if __name__ == "__main__": # 修改税率(类方法) product.update_tax_rate(0.15) # 创建商品 book = book("python编程", 59.99, 100, "john doe") phone = electronics("智能手机", 2999.99, 50, 24) # 显示商品信息(实例方法) print(book.display_info()) print("\n" + phone.display_info()) # 销售商品(实例方法) print("\n" + book.sell(2)) print(phone.sell(1)) # 计算折扣(静态方法) discounted_price = product.calculate_discount(phone.price, 0.2) print(f"\n手机8折价: ¥{discounted_price:.2f}") # 访问私有属性(通过property) print(f"\n图书id: {book.id}") # print(book.__id) # 报错,无法直接访问私有属性
五、最佳实践建议
合理使用方法类型:
- 需要访问实例状态 → 实例方法
- 需要操作类状态 → 类方法
- 独立工具函数 → 静态方法
- 定义接口规范 → 抽象方法
命名约定:
- 普通方法:小写加下划线
calculate_total
- 私有方法:双下划线开头
__internal_logic
- 魔术方法:双下划线开头和结尾
__str__
封装原则:
- 将不需要外部访问的方法设为私有
- 使用property控制属性访问
- 通过方法暴露必要功能而非直接访问属性
文档说明:
- 使用docstring说明方法用途和参数
- 明确哪些方法是公开api,哪些是内部实现
通过本文的学习和案例实践,你应该已经掌握了python中各种方法类型的特点和使用场景。记住,选择合适的方法类型可以使你的代码更加清晰、可维护和符合python风格。
以上就是python类中方法种类与修饰符详解的详细内容,更多关于python方法种类与修饰符的资料请关注代码网其它相关文章!
发表评论