Class refers to: a definition that describes a thing, which is an abstract concept
Instance refers to: a specific individual of this kind of thing, which is a specific thing
For example:
"Person" is a class. "Zhang San" is a specific example of human beings
The same is true in programming. You first define a "class" yourself. When you need to use it, use the definition of "class" to create a specific example. .
Using the definition of a class to create an instance is called instantiation of the class.
The instantiation process is to call the constructor of the class to complete the data allocation of the instance
The following is an example of a washing machine to demonstrate:
This is a simple manual washing machine w instantiation Washer class:
1 #class Washer: 2 class Washer:#定义一个Washer类 3 4 def __init__(self): #这是定义类的构造函数,也是一个特殊的实例方法 5 self.water=0 #实例的属性(变量) 6 self.scour=0 7 8 def add_water(self,water):#实例的方法(函数),可以接受实例化后传过来的参数给类内实例变量,又因为实例继承了类的属性和方法,所以 9 #相当于传递给了类的实例化对象的实例变量10 print('Add water:',water)11 self.water=water#在类的方法内引用实例变量用 self.属性名12 13 def add_scour(self,scour):14 self.scour=scour15 print('Add scour:',scour)16 17 def start_wash(self):18 print('Start wash...')19 20 if __name__=='__main__':21 w=Washer()#类的实例化22 w.add_water(10)#传递参数到实例变量23 w.add_scour(2)24 w.start_wash()
Execution result:
##Instance methods defined in the class need to be added manually The self parameter (as in line 4), but you do not need to manually add the self parameter when calling this method after the class is instantiated, python will automatically add it, as in line 22. The instantiation method is w=Washer(). It can be seen that a parentheses need to be added after the class. You can use instance name.method name() outside the class to call instance methods defined within the class, for examplew.add_scour(2),必要的时候加上参数。
1 class Washer: 2 3 def __init__(self): 4 self.water=0 5 self.scour=0 6 7 def set_water(self,water): 8 self.water=water 9 self.add_water()10 11 def set_scour(self,scour):12 self.scour=scour13 self.add_scour()#在类内调用函数,用 self.方法名14 15 def add_water(self):16 print('Add water:',self.water)17 18 def add_scour(self):19 print('Add scour:',self.scour)20 21 def start_wash(self):22 print('Start wash...')23 24 if __name__=='__main__':25 w=Washer()26 w.set_water(10)27 w.set_scour(2)28 w.start_wash()29
1 class Washer: 2 3 def __init__(self): 4 self.water=0 5 self.scour=0 6 7 def set_water(self,water): 8 self.water=water 9 10 def set_scour(self,scour):11 self.scour=scour 12 13 def add_water(self):14 print('Add water:',self.water)15 16 def add_scour(self):17 print('Add scour:',self.scour)18 19 def start_wash(self):20 self.add_water()21 self.add_scour()22 print('Start wash...')23 24 if __name__=='__main__':25 w=Washer()26 w.set_water(10)27 w.set_scour(2)28 w.start_wash()
1 class Washer: 2 3 def __init__(self): 4 self.water=10#将实例属性修改为默认的标准洗衣程序需要的量 5 self.scour=2 6 7 def set_water(self,water): 8 self.water=water 9 10 def set_scour(self,scour):11 self.scour=scour 12 13 def add_water(self):14 print('Add water:',self.water)15 16 def add_scour(self):17 print('Add scour:',self.scour)18 19 def start_wash(self):20 self.add_water()21 self.add_scour()22 print('Start wash...')23 24 if __name__=='__main__':25 w=Washer()26 ## w.set_water(10) #这两行代码注释掉,代表用户不必设置水和洗涤剂量27 ## w.set_scour(2)28 w.start_wash()29
1 class Washer: 2 3 def __init__(self,water=10,scour=2): 4 self.water=water 5 self.scour=scour 6 7 def set_water(self,water): 8 self.water=water 9 10 def set_scour(self,scour):11 self.scour=scour 12 13 def add_water(self):14 print('Add water:',self.water)15 16 def add_scour(self):17 print('Add scour:',self.scour)18 19 def start_wash(self):20 self.add_water()21 self.add_scour()22 print('Start wash...')23 24 if __name__=='__main__':25 w=Washer()26 w.start_wash()27 28 wb=Washer(100,10)29 wb.start_wash()30 31 wc=Washer(100,10)32 wc.set_water(50)33 wc.set_scour(5)34 wc.start_wash()35 36 37 38 39
Run result:
The constructor can make the instance object more abundant when the class is instantiated, and has the function of instantiating different types of instances.
The above is the detailed content of Introduction to how to use the constructor. For more information, please follow other related articles on the PHP Chinese website!