Use of python classes (class definition, constructor, class attributes, methods), python Constructor

Source: Internet
Author: User

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 ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.