1、方法method
1、在类的作用域中定义的函数,叫做方法。方法是特殊的函数。
初始化方法:__init__,初始化函数
class Mobile:
can_call = True
def __init__(self,color,brand):
self.color = color
self.brand = brand
def sell(self):
print("某部手机被卖了")
def call(self):
print("某部手机正在打电话")
def take_pictrue(self):
print("某部手机正在拍照")
mobile = Mobile("apple","土豪金")
print(mobile.color)
sell、call、take_pictrue都是方法,他们都有一个共同的属性self参数。
把带有self固定参数的方法叫做实例方法。
2、实例方法的调用。
只能有实例调用,类不能调用实例方法。
调用的格式:对象.方法()
class Mobile:
can_call = True
def sell(self):
print("某部手机被卖了")
mobile = Mobile()
mobile.sell()
输出:某部手机被卖了
3、当方法有参数时,遵循和普通函数一样的规则,该传参就传参。
例1:
class Mobile:
can_call = True
def sell(self,money):
print(f"某部手机被卖了{money}元")
mobile = Mobile()
result = mobile.sell(3000)
print(result)
输出:某部手机被卖了3000元
None
__init__没有返回值,其他方法可以有返回值。
例2:
class Mobile:
can_call = True
def sell(self,money):
print(f"某部手机被卖了{money}元")
return money
mobile = Mobile()
result = mobile.sell(3000)
print(result)
输出:某部手机被卖了3000元
3000
例3:
class Mobile:
can_call = True
def sell(self,money,discount = 1):
print(f"某部手机被卖了{money}元")
return money * discount
mobile = Mobile()
result = mobile.sell(3000,0.8)
print(result)
输出:某部手机被卖了3000元
2400.0
sell方法有两个参数,一个位置参数,一个默认参数。传参时如果只传3000,discount默认为1,结果为3000。传两个参数3000,0.8,discount为0.8,结果为2400。
4、在类中一个方法可以调用另一个方法,用self.方法()
class Mobile:
can_call = True
def __init__(self,brand,color):
self.brand = brand
self.color = color
def call(self):
print(f"某部{self.brand}手机正在打电话")
mobile.take_pictrue()
def take_pictrue(self):
print("某部手机正在拍照")
mobile = Mobile("apple","土豪金")
mobile.call()
输出:某部apple手机正在打电话
某部手机正在拍照
5、类方法
class Mobile:
can_call = True
# 声明类方法
@classmethod
def abc(cls):
print(f"这个{cls}类正在使用abc")
mobile = Mobile()
# 调用类方法
Mobile.abc()
mobile.abc()
输出:这个
这个
类方法必须要声明,类可以调用,实例也可以调用
6、静态方法
class Mobile:
can_call = True
# 声明静态方法
@staticmethod
def baozhuang():
print("正在打印")
mobile = Mobile()
# 调用
Mobile.baozhuang()
mobile.baozhuang()
输出:正在打印
正在打印
静态方法没有固定参数,和类和对象没有直接的关联。不能通过self.属性调用实例方法,也不能通过cls.属性调用类方法。
静态方法只是一个普通函数,只不过放在类里面进行管理。
7、类属性和实例属性的区别
(1)类属性是整个类的属性,所有的成员都具备该属性。实例属性只属于某个成员,个体。
(2)类属性可以用类.属性 对象.属性调用,实例属性只能用对象.属性调用。
8、实例方法、类方法的区别于类属性和实例属性区别一致,静态方法与实例方法和类方法没有直接的关联。
一个人,有头,有脚,有手,这些就是人的属性方法:一个人会跑,会跳,这还是方法
2、继承
当自己有对应的属性和方法,优先使用自己的。如果自己没有,会网上一层一层的找。
1、继承
class Mobile:
can_call = True
def call(self):
print("普通手机正在打电话")
# Mobile为父类,SmartPhone为子类。继承父类
class SmartPhone(Mobile):
pass
# 父类的所有属性和方法,子类几乎都可以使用
xiaomi = SmartPhone()
xiaomi.call()
输出:普通手机正在打电话
2、方法重写:重写父类的方法
class Mobile:
can_call = True
def call(self):
print("普通手机正在打电话")
# SmartPhone有自己的call方法,会覆盖父类的call方法
class SmartPhone(Mobile):
def call(self):
print("智能手机正在打电话")
xiaomi = SmartPhone()
xiaomi.call()
输出:智能手机正在打电话
3、三重继承
class Mobile:
can_call = True
def call(self):
print("普通手机正在打电话")
class SmartPhone(Mobile):
pass
# Iphone会先继承父类SmartPhone的属性,父类没有自己的属性,那么Iphone会继承Mobile的属性
class Iphone(SmartPhone):
pass
xiaomi = SmartPhone()
xiaomi.call()
iphone = Iphone()
iphone.call()
输出:普通手机正在打电话
普通手机正在打电话
4、三重继承+重写
class Mobile:
can_call = True
def call(self):
print("普通手机正在打电话")
class SmartPhone(Mobile):
def call(self):
print("智能手机正在打电话")
class Iphone(SmartPhone):
def call(self):
print("苹果手机正在打电话")
xiaomi = SmartPhone()
xiaomi.call()
iphone = Iphone()
iphone.call()
输出:智能手机正在打电话
苹果手机正在打电话
5、多重继承
class Mobile:
can_call = True
def call(self):
print("普通手机正在打电话")
class SmartPhone(Mobile):
def call(self):
print("智能手机正在打电话")
class Musicplayer:
def play_music(self):
print("正在播放音乐")
# Iphone 同时继承SmartPhone和Musicplayer的方法
class Iphone(SmartPhone,Musicplayer):
def call(self):
print("苹果手机正在打电话")
iphone = Iphone()
iphone.call()
iphone.play_music()
输出:苹果手机正在打电话
正在播放音乐
6、超继承super
多重继承+方法重写 super().方法名() 调用父类的方法。
class Mobile:
can_call = True
def call(self):
print("普通手机正在打电话")
class SmartPhone(Mobile):
def call(self):
# 固定写法,表示会先继承父类的call方法,在执行自己的
super().call()
print("智能手机正在打电话")
xiaomi = SmartPhone()
xiaomi.call()
输出:普通手机正在打电话
智能手机正在打电话
不能使用self.call(),self会调用他自己的call方法,不会调用父类的。函数自己调自己会发生死循环。
3、动态获取属性
1、getattr
class Mobile:
color = "黑色"
# 获取属性
print(Mobile.color)
# 使用getattr 获取属性
print(getattr(Mobile,"color"))
输出:黑色
黑色
两种获取属性的区别:getattr,属性可以作为字符串传进来。具体传什么属性可以根据用户的输入控制。
class Mobile:
color = "黑色"
pop_name = input("请输入属性名:")
print(getattr(Mobile,pop_name))
输出:请输入属性名:
输入color ,打印:黑色
2、setattr
设置属性
class Mobile:
color = "黑色"
# 设置属性方法1
Mobile.logo1 = "apple"
# 设置属性方法2 logo2是属性名称,xiaomi是属性值
setattr(Mobile,"logo2","xiaomi")
print(Mobile.logo1)
print(Mobile.logo2)
输出:apple
xiaomi



