Use Python class (the class definition, constructors, class attributes, methods)

# ! / Usr / bin / env Python 
# - * - Coding: UTF-8 - * - 
"" " 
@file: class using .py     
@ E-mail: [email protected] 
@time: 2020/4/4 5:17 pm 
@author: Nobita    
@Version: 1.0    
@Desciption: using Python class (the class definition, constructors, class attributes, methods) 
"" " 


class cltdy:   # define classes, and a name 
    n-1000 =   # class property, class variables within the 

    DEF  the __init__ (Self, name, Age, Profession = ' the IT workers ' ):   # constructor receives an external incoming class constructor parameters depend on 
        the self.name = name 
        self.age = Age 
        self.profession =Profession 

    DEF printing_name (Self):   # class method 
        Print ( ' My name is: {} ' .format (self.name)) 

    DEF printing_age (Self):
         Print ( ' my age is: {} ' .format ( self.age)) 

    DEF printing_profession (Self):
         Print ( ' my career is: {} ' .format (self.profession)) 


# class instantiation, the parameter into classes, but do not pass the parameters can be more may be less than the class constructor parameters (except for the self argument, self is instantiated variable name passed class) 
Test cltdy = ( ' charseki ' , ' 25 ' , ' Doctor' )
 Print ( ' This is the class of the instance of the memory address: {} ' .format (Test)) 
test.printing_name ()   # call when the class is instantiated Method 
test.name = ' charseki1994 '   # can modify the constructor (value of the modified attribute instance) parameters in 
test.printing_name () 
test.printing_profession () 
Print (test.n) 
test.n = 2000   # modify class properties, only for example of commencement test 
Print (test.n, ' \ n-============================================ ' ) 

test2 cltdy = ( ' nobita ' , ' 27 ' ,' Teacher ' )   # instance of an object class named test2 
Print (test2.n)   # print result 1000, not 2000, as modified above class attribute, only for example of commencement test, regardless of the test2 instance. 
test2.printing_age ()

 

Guess you like

Origin www.cnblogs.com/chenshengkai/p/12632845.html