赞
踩
__init__(self)
——这是一个构造方法这是一个特殊方法,类创建时会自动执行它,它必须包含一个self
参数且该参数位于第一个;self
参数指向实例本身的引用,用于访问类中的属性和方法。方法在调用是自动传递实际参数self
class Geese:
def __init__(self):
print("创建 `__init__(self)`——这是一个构造方法")
wildGoose = Geese()
class SelfTest:
def test(self,CS):
print(CS)
HiSelfTest = SelfTest()
HiSelfTest.test("你好啊self") #对的,self是对象本身且自动传递,无需手动传递
# SelfTest.test("你好啊self") #错的,没有传递对象本身
SelfTest.test(HiSelfTest,"你好啊self") #对的,手动传递对象本身
class SelfTest:
def test(self,CS):
print(CS)
def FJ(self):
self.test("CS")
HiSelfTest = SelfTest()
讲解链接:https://www.bilibili.com/video/BV1rB4y1u7mT/?spm_id_from=333.337.search-card.all.click
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。