1. What is object-oriented programming?
---previously used functions
---class + objects
2. What is a class and what is an object and what does it matter?
Class:
def function 1 ():
Pass
def function 2 ():
Pass
#obj is an object, an instantiated process
obj = Class ()
obj. function 1 ()
########## object-oriented bad #########
########## function Programming Good ###########
Sometimes, function programming can be realized, more troublesome;
Object-oriented can be very short and fast implementation;
3. When should I apply object-oriented?
---Multiple parameters can be encapsulated into object-oriented if multiple functions need to pass in multiple common parameters
---to create something from a template
---application scenarios
#分别采用面向对象和函数的方法实现 "Connect the server, upload files and execute commands, turn off the connection" class SSH: def __init__ (self,host,port,pwd,username): Self.host = host ... def connection (self): # Create a connection Self.conn = and fog floats create a linked object () def close (self): # Close self.conn. def upload (self): self.conn. Upload file with link def cmd (self): self.conn. Execute command with link obj = SSH (...) Obj.connection () obj.upload () Obj.cmd () Obj.close () #-----------------functional Programming-------------------DEF upload (): # Connect Server # upload file # Close server def cmd (): # Connect Server # Execute command # Close Server
Python Learning Path: object-oriented supplement