目录
一、多态
1.多态定义理解
2.实例属性和类属性
3.类相关的函数
(1) 实例方法
(2)类方法
(3)静态方法
一、多态
1.多态定义理解
在Python中,多态是一种特性,类似于一个接口,允许在一个对象中的一个操作可作用在不同类型的对象上,执行的不同的任务。
示例:
# 定义一个文具的类
class Stationery(object):
# 定义一个study的方法(函数)
def study(self):
print("一些学习用品")
# 定义一个Rule类,继承自 Stationery 类
class Rule(Stationery):
def study(self): # 重写父类的study方法
print("学习三角板的用法")
# 定义一个Compass类,继承自 Stationery 类
class Compass(Stationery):
def study(self):
print("学习圆规的用法")
# 定义一个 draw 函数,然后接受Stationery类的一个实例
def draw(line):
line.study()
rule = Rule() # 创建 Rule 类的一个实例
draw(rule) # 调用 draw 函数并传入 rule,并调用 Rule 类的 study 方法
compass = Compass()
draw(compass)
以上段代码中,draw 函数接受一个参数 line,这个参数可以是 Stationery类或其任何子类的实例。draw函数中的 line.study() 调用将根据传入对象的实际类型来执行相应的study方法。这就是多态的表现。
2.实例属性和类属性
可参考这篇中“类的定义”理解
https://blog.csdn.net/le_u_6/article/details/139290390?spm=1001.2014.3001.5502https://blog.csdn.net/le_u_6/article/details/139290390?spm=1001.2014.3001.5502
3.类相关的函数
在Python中,根据使用需求的所不同,可将类相关的方法(函数)分为:实例方法、类方法和静态方法。
(1) 实例方法
实例方法是与类实例(对象)相关联的方法。它们通常用于操作或访问类的实例属性。实例方法使用def关键定义,并且其第一个参数通常是self,代表当前的实例对象。
示例:
class Clothes:
# 定义初始化的四个实例属性 :size, brand, cost,price
def __init__(self, size, brand, price):
self.size = size # 实例属性赋值
self.brand = brand # 实例属性赋值
self.cost = 50 # 成本价初始化为50
self.price = price
# 以下定义三个实例方法:on_season,off_season,current_price
def on_season(self, rise):
self.price = self.cost + rise # 计算旺季时的价格
def off_season(self, fall):
self.price = self.cost + fall # 计算淡季时的价格
def current_price(self):
print("当前价格:", self.price, "CNY") # 计算当前总价格
return self.price
# 创建Clothes类的一个c1实例
c1 = Clothes("XL码", "NIKE", None)
# 给c1实例动态添加rise属性,并赋值为200
c1.rise = 200
# 调用on_season方法,并传入c1.rise作为涨价额度
c1.on_season(c1.rise)
# 调用current_price方法,输出当前价格
c1.current_price()
# 创建Clothes类的一个c2实例
c2 = Clothes("XL码", "ANTA",None)
c2.fall = 5
# 调用off_season方法,并传入c2.fall作为降价额度
c2.off_season(c2.fall)
c2.current_price() # 快买!
在三个实例方法内部中,使用self关键字引用当前实例对象的属性,通过对属性的操作实现对价格的调整。
(2)类方法
类方法是与类本身相关联的方法,而不是类的实例。它们通常用于操作类属性或需要与类相关的功能。类方法使用@classmethod装饰器定义,并且其第一个参数通常是 cls,代表类本身。
例:
class Clothes:
# 定义一个类属性,用于记录衣物的总件数
total_clothes = 0
# 定义两个实例属性,用于记录衣物的尺寸和品牌
def __init__(self, size, brand):
self.size = size
self.brand = brand
# 每次创建衣物实例时,总数增加1
Clothes.total_clothes += 1
# 类方法,用于获取衣物的总数
@classmethod
def get_total_clothes(cls):
# 返回衣物的总数
return cls.total_clothes
# 创建两个衣物实例
c1 = Clothes("XXL码", "NIKE")
c2 = Clothes("XL码", "ANTA")
# 当前的衣物总数
print(Clothes.get_total_clothes(), "件")
(3)静态方法
静态方法是与类相关联的方法,但它们不接收类或实例的引用作为参数。它们通常用于工具函数或与类和实例无关的逻辑。静态方法使用@staticmethod装饰器定义,它们可以看作是类内部的普通函数。
例:
class Clothes:
# 定义4个初始化的实例属性
def __init__(self, size, brand, item, number):
self.size = size
self.brand = brand
self.item = item # 单价
self.number = number # 数量
@staticmethod # 静态方法
def get_total_price(item, number):
# 计算总价
return item * number
# 创建一个衣物实例
c = Clothes("XL码", "ANTA", 45, 100)
# 调用静态方法 get_total_price ,并传入单价和数量
total_price = Clothes.get_total_price(c.item, c.number)
# 最后计算出的总金额
print("总额:", total_price, "CNY")
综上所述,装饰器是区分这三种方法的一个显著标志:
实例方法没有特殊的装饰器,通常以 self 作为第一个参数。
类方法使用 @classmethod 装饰器,通常以 cls 作为第一个参数。
静态方法使用 @staticmethod 装饰器,因为没有self参数,所以它无法访问类的实例属性。也由于没有cls参数,所以它也无法访问类属性。因此,静态方法没有啥必须的参数...
实际使用中,选择哪种方法取决于个人的需求:
如果需要操作实例属性或需要实例上下文,使用实例方法。
如果需要访问类属性或执行与类相关的操作,使用类方法好。
如果需要一个与类和实例都无关的工具函数,可使用静态方法。
P.S.
胖中秋,盼国庆......(`へ´*)ノ