python中__init__()方法详情
作者:代号零零一 时间:2023-06-05 21:22:46
python
类中定义的函数称为方法, init ()方法本质也是一个函数。这个函数的作用是初始化实例后的对象。
具体如下例:
init()
方法的作用是初始化实例后的对象cqueue
。
class CQueue:
类中的函数称为方法 ,这里的__init__()
方法在类实例化是被自动调用。若类定义了__init__()
方法,类的实例化操作会自动调用__init__
方法。
__init__
方法可以有参数,参数通过__init__()
传递到类的实例化操作上。self
代表的是类的实例,而非类。
类的方法和普通函数的区别只有一个,类的方法必须有一个额外的第一个参数名称,按照惯例它的名称是self
。
class CQueue
def __init__(self):
self.A,self.B=[],[]
def appendTail(self,value:int)->None:
self.A.append(value)
def deleteHead(self)->int:
if self.B:
return self.B.pop()
if not self.A:
return -1
while self.A:
self.B.append(self.A.pop())
print(self.B)
return self.B.pop()
if __name__=="__main__":
#类CQueue的实例化,这里的括号()不能丢。
#__init__()方法的作用是初始化实例后的对象cqueue。
cqueue=CQueue()
#调用类中的appendTail()函数,增加元素。
cqueue.appendTail(243)
cqueue.appendTail(2)
cqueue.appendTail(4)
cqueue.deleteHead()
cqueue.deleteHead()
有无__init__的区别,结论如下:
1.在类实例化时,如果类中有__init__
方法,此时调用__init__
方法。
2.若该类没有父类,也没有__init__
方法,则不会有任何操作。即对象不会被初始化。
3.若该类有父类,则去调用该类的父类中__init__
方法。
具体实验如下:
定义三个类:CQueue
、student
、sum
,其中CQueue
、student
是sum
的父类。
实验1说明:
__init__
方法的作用是初始化实例的类。实验2说明:当子类没有__init__方法时,会在父类中进行初始化。
实验3,4说明:
sum(student,CQueue )
,子类sum
没有__init__方法时,会调用父类,并且调用父类中排在前面的父类,这里student类在CQueue前,所以调用student
类中的__init__
方法初始化实例的类。实验5说明:在类及其父类均没有初始化时,实例化的类不会被初始化。
实验1代码:
class CQueue:
#类中的函数称为方法 ,这里的__init__()方法在类实例化是被自动调用。若类定义了__init__()方法,类的实例化操作会自动调用__init__方法。
#__init__方法可以有参数,参数通过__init__()传递到类的实例化操作上。self代表的是类的实例,而非类。
#类的方法和普通函数的区别只有一个,类的方法必须有一个额外的第一个参数名称,按照惯例它的名称是self。
def __init__(self):
self.A,self.B=[],[]
def appendTail(self,value:int)->None:
self.A.append(value)
def deleteHead(self)->int:
if self.B:
return self.B.pop()
if not self.A:
return -1
while self.A:
self.B.append(self.A.pop())
print(self.B)
return self.B.pop()
class student:
def __init__(self):
print("测试输出1")
def test1(self):
print("测试输出2")
class sum(student,CQueue ):
def __init__(self):
print("测试输出3")
def test2(self):
print("测试输出4")
if __name__=="__main__":
#类CQueue的实例化,这里的括号()不能丢。
s=sum()
实验结果:
实验2代码:
class CQueue:
#类中的函数称为方法 ,这里的__init__()方法在类实例化是被自动调用。若类定义了__init__()方法,类的实例化操作会自动调用__init__方法。
#__init__方法可以有参数,参数通过__init__()传递到类的实例化操作上。self代表的是类的实例,而非类。
#类的方法和普通函数的区别只有一个,类的方法必须有一个额外的第一个参数名称,按照惯例它的名称是self。
def __init__(self):
self.A,self.B=[],[]
def appendTail(self,value:int)->None:
self.A.append(value)
def deleteHead(self)->int:
if self.B:
return self.B.pop()
if not self.A:
return -1
while self.A:
self.B.append(self.A.pop())
print(self.B)
return self.B.pop()
class student:
def __init__(self):
print("测试输出1")
def test1(self):
print("测试输出2")
class sum(student,CQueue ):
# def __init__(self):
# print("测试输出3")
def test2(self):
print("测试输出4")
if __name__=="__main__":
#类CQueue的实例化,这里的括号()不能丢。
s=sum()
实验结果:
实验3,4,省略。
实验5:
class CQueue:
#类中的函数称为方法 ,这里的__init__()方法在类实例化是被自动调用。若类定义了__init__()方法,类的实例化操作会自动调用__init__方法。
#__init__方法可以有参数,参数通过__init__()传递到类的实例化操作上。self代表的是类的实例,而非类。
#类的方法和普通函数的区别只有一个,类的方法必须有一个额外的第一个参数名称,按照惯例它的名称是self。
def __init__(self):
self.A,self.B=[],[]
def appendTail(self,value:int)->None:
self.A.append(value)
def deleteHead(self)->int:
if self.B:
return self.B.pop()
if not self.A:
return -1
while self.A:
self.B.append(self.A.pop())
print(self.B)
return self.B.pop()
class student:
# def __init__(self):
# print("测试输出1")
def test1(self):
print("测试输出2")
class sum(student,CQueue ):
# def __init__(self):
# print("测试输出3")
def test2(self):
print("测试输出4")
if __name__=="__main__":
#类CQueue的实例化,这里的括号()不能丢。
s=sum()
实验结果:
从实验结果可以看出,没有任何操作。
来源:https://blog.csdn.net/qq_37993640/article/details/121532933
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于python之字典的嵌套,递归调用方法
javascript同步服务器时间和同步倒计时小技巧
![](https://img.aspxhome.com/file/2023/6/56116_0s.jpg)
css基础教程属性篇
![](https://img.aspxhome.com/file/UploadPic/20087/23/200872312564485s.jpg)
MySQL系列数据库设计三范式教程示例
![](https://img.aspxhome.com/file/2023/5/118175_0s.jpg)
如何使用PyCharm将代码上传到GitHub上(图文详解)
![](https://img.aspxhome.com/file/2023/2/83652_0s.png)
获取SQL Server数据库元数据的几种方法
MySQL Server 层四个日志详解
![](https://img.aspxhome.com/file/2023/5/95625_0s.jpg)
python从入门到精通(DAY 3)
![](https://img.aspxhome.com/file/2023/8/63598_0s.png)
CentOS中升级Python版本的方法详解
浅析python中while循环和for循环
mysql5.7版本root密码登录问题的解决方法
![](https://img.aspxhome.com/file/2023/3/122913_0s.png)
JS实现课程表小程序(仿超级课程表)加入自定义背景功能
![](https://img.aspxhome.com/file/2023/9/136589_0s.gif)
mysql limit查询优化分析
MySQL root密码的重置方法
![](https://img.aspxhome.com/file/2023/0/124550_0s.png)
MySQL授权命令grant的使用方法小结
解析smarty模板中类似for的功能实现
Python标准库学习之psutil内存详解
![](https://img.aspxhome.com/file/2023/8/112138_0s.png)
利用Python的turtle库绘制玫瑰教程
![](https://img.aspxhome.com/file/2023/4/86674_0s.jpg)
Python如何使用ElementTree解析xml
![](https://img.aspxhome.com/file/2023/0/130800_0s.png)