5 ----- class and object class methods, static methods

1, the general method: bound to an object, without any decoration, the object automatically ( Self ) passed to the function

2, class methods: a method @classmethod decorated, automatically class ( CLS ) as the first parameter to the function, the object may be invoked

3, static methods: the method @staticmethod decorated, does not automatically pass self or cls, it is an ordinary functions, classes, and objects can call

#
 The HOST = ' 127.0.0.1 ' 
PORT = 3306 
DB_PATH = R & lt ' C: \ the Users \ Administrator \ PycharmProjects \ Test \ OOP \ test1 \ DB ' 

class Mysql:
     DEF  the __init__ (Self, Host, Port): 
        self.host = Host 
        self.port = Port 
    @classmethod 
    DEF from_conf (CLS):
         Print (CLS)
         return CLS (the HOST, PORT) # from the configuration file to initialize 
    DEF Fun (Self):
         Print ( ' self.fun () ', Self) 
    @staticmethod 
    DEF fun_static ():
         Print ( ' form fun_static ... ' ) 

Print (Mysql.from_conf ())
 Print ( ' -------- ' ) 
Conn = Mysql.from_conf () 
Conn. from_conf () # objects can also be invoked, but the first pass remains the default parameter class, do not make calls normally go 
Print ( ' -------- ' ) 
m = Mysql (the HOST, PORT) 
Mysql .fun (m) 
Print ( ' -------- ' )
 Print (m.fun_static ())

 

Guess you like

Origin www.cnblogs.com/cc-world/p/12649741.html