Use of python classes (class definition, constructor, class attributes, methods), python Constructor
Note: Here we only describe how to use the method. The concept of a specific class should not be difficult for me.
In a word, a programming language is just a tool and can be used. I think it is necessary to study C, assembly, and electronic links.
1 class cltdy: # defines the class and starts a class name 2 n = 1000 # class attribute. The variable 3 def _ init _ (self, name, age, permission sion = 'it migrant workers'): # constructor. The class receives external input parameters and relies entirely on the constructor 4 self. name = name 5 self. age = age 6 self. sion = sion 7 def printing_name (self): # Class Method 8 print ('My name is: % s' % self. name) 9 def printing_age (self): 10 print ("My age: % s" % self. age) 11 def printing_pfsn (self): 12 print ("My occupation: % s" % self. permission sion) 13 14 test = cltdy ('sober', 25, 'devops ') # class instantiation, passing parameters into the class, the input parameters can be many but not less than the parameters of the class Constructor (except for the self parameter, self is to pass the instantiated variable name into the class) 15 print ("this is the memory address after the class is instantiated: % s "% test) 16 test. printing_name () # Call The Method 17 test in the class after instantiation. name = 'Moon '# You can modify the parameter value 18 test in the constructor. printing_name () 19 test. printing_pfsn () 20 print (test. n) 21 test. n = 2000 # modify the class attributes and take effect only for test instantiation 22 print (test. n, '\ n =============================') 23 24 t2 = cltdy ('jack', 22, 'Student ') # instantiate a class object named t225 print (t2.n) 26 t2.printing _ age ()