带你了解图像篡改检测的前世今生
570 2023-04-03 04:26:55
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
的父类。
__init__
方法的作用是初始化实例的类。sum(student,CQueue )
,子类sum
没有__init__方法时,会调用父类,并且调用父类中排在前面的父类,这里student类在CQueue前,所以调用student
类中的__init__
方法初始化实例的类。实验1代码:
?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465class
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代码:
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667class
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:
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667class
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
()
实验结果:
从实验结果可以看出,没有任何操作。
到此这篇关于 python中__init__()方法详情的文章就介绍到这了,更多相关 python中__init__()方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章: