1.方法的定义
类和对象中:成员所具有的特征我们叫做属性,而成员的行为,就是实例方法。
在类的作用域里面定义的函数,就叫方法,是特殊的函数。
比如初始化方法__init__,我们也叫作初始化函数。
把带有 self 固定参数的方法叫做实例方法,这个方法属于对象/实例的方法。
2.实例方法的调用
1)实例方法的调用,只能由实例调用,类不能调用实例方法。
语法:对象.方法()
# 创建对象
mobile = Mobile('apple', 'pink')
# 对象调用实例方法
mobile.sale() ==> 某部手机被卖掉了
# 类调用实例方法
Mobile.sale() ==> TypeError: sale() missing 1 required positional argument: 'self'
class Mobile:
# 类属性:所有成员的共同特征
# call = True
def __init__(self, brand, color):
# 实例属性:个体特征
self.brand = brand
self.color = color
def sale(self):
print('某部手机被卖掉了')
def call(self):
print('某部手机正在打电话')
def take_pictures(self):
print('某部手机正在拍照')
# 创建对象
mobile = Mobile('apple', 'pink')
# 实例方法的调用,只能由实例调用,类不能调用实例方法
# 对象.方法()
mobile.sale()
# 类不能调用实例方法,报错 TypeError: sale() missing 1 required positional argument: 'self'
# Mobile.sale()
运行结果:
D:\Python3.7\python.exe D:/PycharmProjects/Py44/test.py
某部手机被卖掉了Process finished with exit code 0
2)__init__方法没有返回值,但是其他实例方法可以有返回值
class Mobile:
# 类属性:所有成员的共同特征
# call = True
def __init__(self, brand, color):
# 实例属性:个体特征
self.brand = brand
self.color = color
def sale(self, price,discount=1):
print(f'某部手机原价{price}元')
return price * discount
def call(self):
print('某部手机正在打电话')
def take_pictures(self):
print('某部手机正在拍照')
# 创建对象
mobile = Mobile('apple', 'pink')
# 当方法有参数时,遵循和普通函数一样的规则,传参
# __init__方法没有返回值,但是其他实例方法可以有返回值
result = mobile.sale(3000)
print(f'付款金额{result}元')
运行结果:
D:\Python3.7\python.exe D:/PycharmProjects/Py44/test.py
某部手机原价3000元
付款金额3000元Process finished with exit code 0
如果sale方法没有return,则打印result,返回None。
3)在类里面,一个实例方法可以调用另一个实例方法。
语法:self.方法()
class Mobile:
# 类属性:所有成员的共同特征
# call = True
def __init__(self, brand, color):
# 实例属性:个体特征
self.brand = brand
self.color = color
def sale(self, price,discount=1):
print(f'某部手机原价{price}元')
return price * discount
def call(self):
print('某部手机正在打电话')
self.record()
def take_pictures(self):
print('某部手机正在拍照')
def record(self):
print('正在录音')
# 创建对象
mobile = Mobile('apple', 'pink')
# 在类里面,一个实例方法调用另一个实例方法时,语法:self.方法名()
mobile.call()
运行结果:
D:\Python3.7\python.exe D:/PycharmProjects/Py44/test.py
某部手机正在打电话
正在录音Process finished with exit code 0
4)方法内调用实例属性
语法:self.属性名称
class Mobile:
# 类属性:所有成员的共同特征
# call = True
def __init__(self, brand, color):
# 实例属性:个体特征
self.brand = brand
self.color = color
def sale(self, price,discount=1):
print(f'某部手机原价{price}元')
return price * discount
def call(self):
print('某部手机正在打电话')
self.record()
def take_pictures(self):
# 类里面调用实例属性 语法:self.brand
print(f'一部{self.brand}手机正在拍照')
def record(self):
print('正在录音')
# 创建对象
mobile = Mobile('apple', 'pink')
# 方法中,调用实例属性
mobile.take_pictures()
运行结果:
D:\Python3.7\python.exe D:/PycharmProjects/Py44/test.py
一部apple手机正在拍照Process finished with exit code 0
3.类方法和静态方法
简单了解下即可,一般用不到,可能面试过程中会有涉及。
类方法:类可以调用的方法,定义时需要使用@classmethod进行声明,方法参数cls代表类本身。
语法:
@classmethod def 方法(cls): 方法体
实例可以调用类方法。但类不能调用实例方法。
静态方法:在本质上只是一个普通函数,和类和对象没有直接的关联,只是作用域在类里面,定义时需要使用@staticmethod进行声明,没有固定参数,不能通过self.属性调用实例属性和其他的实例方法 ,也不能通过cls.属性调用类属性和其他的类方法。
语法:
@staticmethod def 方法(): 方法体
class Mobile:
# 类属性:所有成员的共同特征
# call = True
def __init__(self, brand, color):
# 实例属性:个体特征
self.brand = brand
self.color = color
def sale(self, price, discount=1):
print(f'某部手机原价{price}元')
return price * discount
def call(self):
print('某部手机正在打电话')
self.record()
def take_pictures(self):
# 类里面调用实例属性 语法:self.brand
print(f'一部{self.brand}手机正在拍照')
def record(self):
print('正在录音')
# 类方法的定义,@classmethod声明类方法
@classmethod
def cmethod(cls,message):
cls.mseeage = '123'
print(f'这个类{cls}正在使用cmethod,编辑信息{cls.mseeage},{message}')
# 静态方法的定义
# 没有固定参数,和类还有对象没有直接的关联
# 不能通过self.属性调用实例属性和其他的实例方法
# 不能通过cls.属性调用类属性和其他的类方法
@staticmethod
def smethod():
print('测试静态方法')
# 创建对象
mobile = Mobile('apple', 'pink')
# 调用类方法,类和对象都可以调用,但基本上用不到类方法
Mobile.cmethod('test')
mobile.cmethod('test')
# 调用静态方法
Mobile.smethod()
mobile.smethod()
运行结果:
D:\Python3.7\python.exe D:/PycharmProjects/Py44/test.py
这个类<class '__main__.Mobile'>正在使用cmethod,编辑信息123,test
这个类<class '__main__.Mobile'>正在使用cmethod,编辑信息123,test
测试静态方法
测试静态方法Process finished with exit code 0
所有的静态方法都可以移到类外,成为普通的函数。
4.为什么要用类与对象?
举例说明:
1)函数实现
# 函数
name = '泰迪'
gender = 'male'
age = 2
def eating(name, gender, age):
print(f'{name}正在吃狗粮,他的性别是{gender},年龄是{age}')
def bark(name, gender, age):
print(f'{name}正在叫,显然是饿了,他的性别是{gender},年龄是{age}')
def bath(name, gender, age):
print(f'{name}正在洗澡,他的性别是{gender},年龄是{age}')
# 函数的调用
eating(name, gender, age)
bark(name, gender, age)
bath(name, gender, age)
2)类和对象实现
# 类和对象
class Dog:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def eating(self):
print(f'{self.name}正在吃辣条,他的性别是{self.gender},年龄是{self.age}')
def bark(self):
print(f'{self.name}正在叫,显然是饿了,他的性别是{self.gender},年龄是{self.age}')
def bath(self):
print(f'{self.name}正在洗澡,他的性别是{self.gender},年龄是{self.age}')
dog = Dog('泰迪', 'male', 2)
dog.eating()
dog.bark()
dog.bath()
可以看出来,调用过程更简化,但代码行增加。