python - 类和对象

一.类

类名用大写字母开头

属性是类中的变量,方法是类中的函数

类、class关键字:

复制代码
>>> class Turtle:
...     color = 'green'
...     weight = 10
...     legs = 4
...     shell = True
...     mount = '大嘴'
...     def climb(self):
...             print("我正在努力的向前爬")
...     def run(self):
...             print("我正在飞快的向前怕")
...     def bite(self):
...             print("咬死你咬死你")
...     def eat(self):
...             print("有的吃真满足")
...     def sleep(self):
...             print("困了困了")
...
>>> t1 = Turtle()
>>> t1.color
'green'
>>> t1.weight
10
>>> t1.legs
4
>>> t1.shell
True
>>> t1.mount
'大嘴'
>>> t1.climb()
我正在努力的向前爬
>>> t1.run()
我正在飞快的向前怕
>>> t1.bite()
咬死你咬死你
>>> t1.eat()
有的吃真满足
>>> t1.sleep()
困了困了

二.封装

在创建对象之前,通过类将相关的属性和方法给打包到一起。然后再通过类来生成相应的对象

复制代码
>>> class C:
...     def hello():
...             print('hello')
...
>>> c = C()
>>> c.hello()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: C.hello() takes 0 positional arguments but 1 was given

>>> class C:
...     def getSelf(self):
...             print(self)
...
>>> c = C()
>>> c.getSelf()
<__main__.C object at 0x000001536290D2E0>
>>> c
<__main__.C object at 0x000001536290D2E0>

传递到方法中self参数的实参就是实例对象本身

一个类可以生成无数个对象,当调用类中的方法时,python为了明确是哪一个实例对象在调用该方法,于是使用self参数将调用该方法的实例对象的信息进行了传递。

三.继承

当子类继承父类时,子类拥有和父类相同的方法和相同的属性

复制代码
>>> class A:
...     x = 1
...     def hell(self):
...             print('你好')
...
>>> class B(A):
...     def bb(self):
...             print('我是B的对像')
...
>>> b = B()
>>> b.x
1
>>> b.bb()
我是B的对像
>>> b.hell()
你好
>>> class B(A):
...     x = 123
...     def hello(self):
...             print('我是B的对象')
...
>>> b = B()
>>> b.x
123
>>> b.hello()
我是B的对象

isinstance():判断某个对象是否属于某个方法

复制代码
>>> isinstance(b,B)
True
>>> isinstance(b,A)
True

issubclass():检测一个类是否为某个类的子类

复制代码
>>> issubclass(B,A)
True

多重继承

复制代码
>>> class A:
...     x = 1
...     def hello(self):
...             print('你好')
...
>>>
>>> class B():
...     x = 123
...     def hello(self):
...             print('这里是B')
...
>>> class C(A,B):
...     pass
...
>>> c = C()
>>> c.x
1
>>> c.hello()
你好

多重继承时,多个父类拥有同样的属性和方法,子类对象调用时优先级从左至右。

组合:

组合(将需要的类一起进行实例化并放入新的类中),非继承关系的几个类放到一起

复制代码
>>> class A:
...     def hello(self):
...             print('这里是A')
...
>>> class B:
...     def hello(self):
...             print('这里是B')
...
>>> class C:
...     def hello(self):
...             print('这里是C')
...
>>> class ABC:
...     a = A()
...     b = B()
...     c = C()
...     def hello(self):
...             self.a.hello()
...             self.b.hello()
...             self.c.hello()
...
>>> abc = ABC()
>>> abc.hello()
这里是A
这里是B
这里是C

绑定

实例对象跟类的方法进行绑定

实例能拥有自己的属性,如果要通过类的方法对属性进行操作,则需要使用self进行绑定

复制代码
>>> class A:
...     x = 120
...     def set_x(self,i):
...             self.x = i
...
>>> a = A()
>>> a.set_x(520)
>>> a.x
520

若没有这个self,那么直接对x进行操作,只是在set_x()函数内部创建了一个局部变量x

四.构造函数

构造函数__init__()

在类中定义__intit__()方法,就可以在实例化对象的时候实现一些变量的初始化

如果创建类的时候,没有添加构造函数,python解释器会自动创建一个不执行任何操作的默认构造函数;也就是说,只要创建类,一定会伴随着一个构造函数诞生。只不过可以自定义一个构造函数,也可以由python解释器自动创建一个默认的构造函数。

复制代码
>>> class go:
...     def __init__(self, x, y):
...             self.x = x
...             self.y = y
...     def add(self):
...             return self.x + self.y
...     def mul(self):
...             return self.x * self.y
...
>>> a = go(3,4)
>>> a.__dict__
{'x': 3, 'y': 4}
>>> a.add()
7
>>> a.mul()
12

五.重写

子类可以重新定义父类已有的属性和方法来对父类中同名的属性和方法进行覆盖。

可以在子类中直接调用父类的方法,即调用未绑定的父类方法。但会造成钻石继承的问题。

复制代码
>>> class gozi(go):
...     def __init__(self, x, y, z):
...             go.__init__(self, x, y)
...             self.z = z
...     def add(self):
...             return go.add(self) + self.z
...     def mul(self):
...             return self.x * self.y * self.z
...
>>> b = gozi(2, 3, 4)
>>> b.__dict__
{'x': 2, 'y': 3, 'z': 4}
>>> b.add()
9
>>> b.mul()
24

钻石继承问题:

复制代码
>>> class A:
...     def __init__(self):
...             print('这里是A')
...
>>> class B1(A):
...     def __init__(self):
...             A.__init__(self)
...             print('这里是B1')
...
>>> class B2(A):
...     def __init__(self):
...             A.__init__(self)
...             print('这里是B2')
...
>>> class C(B1, B2):
...     def __init__(self):
...             B1.__init__(self)
...             B2.__init__(self)
...             print('这里是C')
...
>>> c = C()
这里是A
这里是B1
这里是A
这里是B2
这里是C

A被调用了两次,因为C调用B1、B2,B1、B2又分别调用了A。

为了解决砖石继承的问题可以使用super()函数。

复制代码
>>> class A:
...     def __init__(self):
...             print('这里是A')
...
>>> class B1(A):
...     def __init__(self):
...             super().__init__()
...             print('这里是B1')
...
>>> class B2(A):
...     def __init__(self):
...             super().__init__()
...             print('这里是B2')
...
>>> class C(B1, B2):
...     def __init__(self):
...             super().__init__()
...             print('这里是C')
...
>>> c = C()
这里是A
这里是B2
这里是B1
这里是C

六.多态

同一个运算符、函数或对象在不同的场景下具有不同的作用效果

运算符的多态

复制代码
>>> 3 + 5
8
>>> 'L' + 'ove'
'Love'

函数的多态

复制代码
>>> len('love')
4
>>> len(['love','jiji','xixi'])
3
相关推荐
一代明君Kevin学长7 分钟前
快速自定义一个带进度监控的文件资源类
java·前端·后端·python·文件上传·文件服务·文件流
HappRobot20 分钟前
python类和对象
开发语言·python
盼哥PyAI实验室33 分钟前
Python YAML配置管理:12306项目的灵活配置方案
开发语言·python
Github掘金计划1 小时前
开发者狂喜!GitHub 官方开源:支持 Copilot/Cursor,规范即代码,27k Star 封神!
java·python·kafka·github·copilot
shenzhenNBA1 小时前
python用openpyxl操作excel-单元格样式操作
python·excel·openpyxl·单元格样式
岁月宁静1 小时前
多模态 Agent 技术全景解析 — 从模型能力、Agent 架构到工程化与商业落地
python·agent
试着1 小时前
【VSCode+AI+测试】连接ai大模型
ide·人工智能·vscode·python·学习·编辑器·ai-test
零小陈上(shouhou6668889)2 小时前
YOLOv8+PyQt5海洋船只检测(可以重新训练,yolov8模型,从图像、视频和摄像头三种路径识别检测)
开发语言·python·yolo
znhy_232 小时前
day36打卡
python
gf13211112 小时前
python_字幕文本、音频、视频一键组合
python·音视频·swift