day18面向对象
1.内置类的属性
class Dog:
“”“狗类”""
num = 20
def __init__(self, name, age=3, color='白色', gender='公'):
self.name = name
self.gender = gender
self.age = age
self.color = color
def show_message(self):
print(self.name, self.gender, self.age, self.color)
@classmethod
def show_num(cls):
print('狗的数量:', cls.num)
@staticmethod
def info():
print('狗是人类的朋友!')
1.doc – 获取类的说明文档(类属性)
print(Dog.doc)
print(int.doc)
2.class – 获取对象类型(对象属性),功能和type()函数一样
print(Dog.__class__)
print(type(Dog))
3.name – 获取类名(类属性)
print(Dog.name)
4.mudule – 获取类所在的模块的模块名(类属性)
print(Dog.module)
- dict – 获取指定的类所有的类属性及其对应的值(以字典形式返回)
dict — 获取指定对象所有的对象属性机器对应的值
print(Dog.dict)
6.base – 获取指定类的父类
bases – 获取指定类的父类们
二.对象属性的增删改查
lass Person:
def __init__(self, name, age):
self.name = name
self.age = age
# 会在当前类的对象被打印的时候自动调用,并且打印结果就是这个函数的返回值(字符串 )
def __repr__(self):
return 'abc'
三.方法
1.方法
‘’’
1)对象方法
怎么定义:直接定义在类中的函数
怎么调用:通过对象参数
特点:有默认参数self,self在调用的时候不用传参,系统将自动对当前
怎么么用:如果实现函数功能需要对象属性就使用对象发方法(对象方法中的self可以用来提供需要的所有对象属性)
2)类方法
怎么定义:在类中定义函数前加装饰器@classmethod
怎么调用:通过类调用
特点:自带参数cls,cls也不需要传参,系统会将当前类传给cls谁调用就指向谁
什么时候用:实现函数功能在不需要对象属性的时候需要类,就是用类方法
3)静态方法
怎么定义:在类型定义函数前加装饰器@staticmethod
怎么调用:通过类调用
特点:没有默认参数
什么时候用:实现函数的功能既不需要类也不需要对象属性就使用静态方法
‘’’
class A:
# 类方法
@classmethod
def func2(cls):
print('类方法')
@classmethod
def func3(cls, x):
print(x)
func4 静态方法
from copy import copy
# 1.python中的运算符
# python中每一个运算符都对应一个固定的魔法方法。每次使用运算符的时候本质就是去调用对应方法。
# 某种类型的数据是否支持某种运算和这个类型中有没有实现运算符对应的魔法方法有关
12 + 20 # 12.__add__(20)
'abc'+'123' # 'abc'.__add__('123')
# 2.在自己的类中重载指定运算符
class Student:
def __init__(self, name, age=18, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return f'<{str(self.__dict__)[1:-1]}>'
# ================运算符重载===================
def __lt__(self, other):
return self.score < other.score
# self是+前面的数据,other是+后面的数据; 返回值是计算结果
def __add__(self, other):
# return self.score + other.score
return [self, other]
def __mul__(self, other):
return [copy(self) for _ in range(other)]
stu1 = Student('小明', score=90, age=40)
stu2 = Student('小红', age=20, score=80)
stu3 = Student('Tom', age=31, score=89)
print(stu1 != stu2) # 默认支持比较相等于
print(stu1 + stu2) # 相当于: print(stu1.__add__(stu2))
print(stu1 + stu3)
result = stu1*3 # stu1.__mul__(3)
print(result)
# del stu1.name
# print(result)
# 按年龄排序
students = [stu1, stu2, stu3]
students.sort()
print(students)@staticmethod
def func4():
print('静态')
A.func2()from copy import copy
# 1.python中的运算符
# python中每一个运算符都对应一个固定的魔法方法。每次使用运算符的时候本质就是去调用对应方法。
# 某种类型的数据是否支持某种运算和这个类型中有没有实现运算符对应的魔法方法有关
12 + 20 # 12.__add__(20)
'abc'+'123' # 'abc'.__add__('123')
# 2.在自己的类中重载指定运算符
class Student:
def __init__(self, name, age=18, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return f'<{str(self.__dict__)[1:-1]}>'
# ================运算符重载===================
def __lt__(self, other):
return self.score < other.score
# self是+前面的数据,other是+后面的数据; 返回值是计算结果
def __add__(self, other):
# return self.score + other.score
return [self, other]
def __mul__(self, other):
return [copy(self) for _ in range(other)]
stu1 = Student('小明', score=90, age=40)
stu2 = Student('小红', age=20, score=80)
stu3 = Student('Tom', age=31, score=89)
print(stu1 != stu2) # 默认支持比较相等于
print(stu1 + stu2) # 相当于: print(stu1.__add__(stu2))
print(stu1 + stu3)
result = stu1*3 # stu1.__mul__(3)
print(result)
# del stu1.name
# print(result)
# 按年龄排序
students = [stu1, stu2, stu3]
students.sort()
print(students)
A.func3(22)
A.func4()
2.方法在定义的时候怎么确定参数
看实现函数的功能除了类中属性以外还需不需要额外的数据,需要几个
3.方法的调用问题
(1)本质上不管什么方法,都可以用对象和类调用
注意:类可以调用对象方法。但是self就必须自己去传参,失去可以指向当前对象的意义
注意:对象可以直接调用类方法,调用的时候cls还是指向类但是会消耗额外的cpu资源
四.运算符重载
1.python中的运算符
python中每一个运算符都对应一个固定的魔法方法。每次使用运算符的时候本质就是去调用对应方法。
某种类型的数据是否支持某种运算和这个类型中有没有实现运算符对应的魔法方法有关
12 + 20 # 12.add(20)
‘abc’+‘123’ # ‘abc’.add(‘123’)
2.在自己的类中重载指定运算符
from copy import copy
class Student:
def __init__(self, name, age=18, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return f'<{str(self.__dict__)[1:-1]}>'
# ================运算符重载===================
def __lt__(self, other):
return self.score < other.score
# self是+前面的数据,other是+后面的数据; 返回值是计算结果
def __add__(self, other):
# return self.score + other.score
return [self, other]
def __mul__(self, other):
return [copy(self) for _ in range(other)]
stu1 = Student('小明', score=90, age=40)
stu2 = Student('小红', age=20, score=80)
stu3 = Student('Tom', age=31, score=89)
print(stu1 != stu2) # 默认支持比较相等于
print(stu1 + stu2) # 相当于: print(stu1.__add__(stu2))
print(stu1 + stu3)
result = stu1*3 # stu1.__mul__(3)
print(result)
del stu1.name
print(result)
按年龄排序
students = [stu1, stu2, stu3]
students.sort()
print(students)