three basic structures of programming: object-oriented, process-oriented, functional programming1, what is object-oriented programming object-oriented programming is a way of programming, this programming method of landing needs to use "class" and "object" to implement, so, object-oriented programming is actually the use of "class" and "object". 2, what is a class what is an object, and what relationship can be said that the class is a collection of functions, look at the following code
class class:# classes name def function 1 () pass def function 2 () Pass
The Obj= class () #创建一个对象, instantiating the process obj. function 1 () #方法 3, when is object-oriented? When we want to write a machine to upload a file and execute the command script, we can use
Process orientedmethod, using the function to write this way:
defupload ():#connecting to a server #Uploading Files #Close Connection Passdefcmd ():#connecting to a server #执行命令 #Close Connection Passdefupload_cmd ():#connecting to a server #Uploading Files #Execute Command #Close Connection Pass
You can do that.
defConnect ():#connecting to a server PassdefClose ():#Disconnect Connection Passdefupload (): Connect ()#Uploading FilesClose ()Passdefcmd (): Connect ()#Execute CommandClose ()Passdefupload_cmd (): Connect () upload () cmd () Close ()Pass
It feels like a bit cumbersome, and it's always handled, so what if you use object-oriented?
classSSH (object):def __init__(self,host,port,pwd,username): Self.host=host ...defconnection (self): self.connection=and server creation objects ()defClose (self):#Closeself.connection. Closedefupload (self): self.connection ()#uploading files using a connection defcmd (self): self.connection ()#using the Execute commandobj= SSH ("', Host,port ....) Obj.connection () obj.upload () Obj.close ( )
We take the public function, extract it, can also create properties belonging to the object, other methods can be used, so it is not better?
There is also a usage scenario, which is similar to templating, and the instance code is as follows:
classRole (object):def __init__(Self, name, role, weapon, life_value=100, money=15000): Self.name=name Self.role=role Self.weapon=Weapon Self.life_value=Life_value Self.money= MoneydefShot (self):Print("%s Shooting ..."%self.name) self.__heart=" die" Print(self.)__heart) defgot_shot (self,):Print("%s Say:ah...,i got shot ..."%self.name)defBuy_gun (Self, gun_name):Print("%s just bought%s"%(self.name,gun_name)) Self.weapon= Gun_name#You can change the properties of an object through a classR1= Role ('Alex','Police','AK47')#generate a roler2 = Role ('Jack','Terrorist','B22')#generate a role
Conclusion:
object-oriented does not write, also can achieve and satisfy the function, is more troublesome, and object-oriented simple can be achieved, but
no matter what form of programming we use, we have to remember the principle clearly:
1. Writing duplicate code is a very bad low-level behavior
2. The code you write needs to be changed frequently, so it needs to be readable and easy to expand.
3. The main difference between process-oriented programming and OOP is that OOP makes programs more extensible and easy to change
The next article summarizes the object-oriented syntax and features:
Python Object-oriented learning 2 (object-oriented syntax and features)
Python object-oriented learning 1 (what is object-oriented, object-oriented scenario)