当前位置:网站首页>day10· __init__方法
day10· __init__方法
2022-08-05 13:44:00 【慵懒的猫丫头】
__init__方法既可以叫初始化方法(对象创建时自动调用),也可以叫构造方法、魔术方法(双下划线+函数名+双下划线的方法)。
对象创建流程
class Test(object):
def __init__(self):
print("我是初始化方法")
def test1(self):
print("我是test1方法")
t = Test()
print(t) # return <__main__.Test object at 0x00000238A6FB7588> 
init方法的作用
希望对象创建时就做的一些操作,可以写在init方法里,通常将对象创建时自动绑定属性写在init方法里
# 一个类可以创建出很多个对象,所有的对象如果属性名是一致的可以直接添加在init方法里
class Test(object):
def __init__(self):
self.title = "child object"
def test1(self):
print("我是test1方法")
t1 = Test()
t2 = Test()
print(t1.title, t2.title) #return child object
# 一个类可以创建出很多个对象,所有的对象属性值不一样,可以让对象属性值作为参数传递给init
class Test(object):
def __init__(self, title):
self.title = title
def test1(self):
print("我是test1方法")
# 在创建对象时就需要将参数传递给init,因为对象创建时底层会自动调用init
t1 = Test("t1")
t2 = Test("t2")
边栏推荐
- 当天期货开户次日就可以交易
- OpenCV 连通分量标记和分析
- Qt实现多国语言切换
- 开户的期货公司各有擅长的领域
- Synchronized锁升级
- 序章 调度系统架构设计总述
- 【CC3200AI 实验教程2】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-系统测试
- @2023 Graduate Candidates: How to Spend the "Golden Period" of Summer Research Exam Preparation
- 115. In-depth explanation of the technical implementation of configuring the local SAP UI5 application to the local Fiori Launchpad
- 内存问题难定位,那是因为你没用ASAN
猜你喜欢
随机推荐
第三章 调度系统架构设计之获取集群资源信息
Qt将图片保存为XML文件或者变为QSting
Synchronized锁升级
配置网络源仓库
ESP8266 做简单的仪器
useEffect默认执行两次解决方案
十分钟教会你如何使用VitePress搭建及部署个人博客站点
DSPE-PEG-Hydrazide,DSPE-PEG-HZ,磷脂-聚乙二醇-酰肼可增强稳定性
115. In-depth explanation of the technical implementation of configuring the local SAP UI5 application to the local Fiori Launchpad
学习笔记61—兴趣阅读之经济学
Source code analysis 2 Model conversion export.py
五、平衡二叉树——伸展树Splay
运力升级助力算力流转,中国数字经济的加速时刻
R语言patchwork包将多个可视化结果组合起来、plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义分割符号信息(separator
Qt实现多国语言切换
DSPE-PEG-Azide,DSPE-PEG-N3,磷脂-聚乙二醇-叠氮具有亲水和疏水性
LeetCode高频题69. x 的平方根,二分法搞定,非常简单
mmap内核实现及物理内存组织结构
国产虚拟化云宏CNware WinStack安装体验-6 集群HA功能测试
Xingyun Butler won the "2022 Outstanding Brand Image Award" at the 11th China Finance Summit









