1. Encapsulation of classes and objects#方法1
def bar (): print "Bar" def Hello (name): print "I am%s"% (name)
#方法2
Class Foo (): def Bar (self): print ' Bar def Hello (self,name): print "I am%s"%name
For database additions and deletions to search # # # #方式1
def fetch (HOSTNAME,PORT,USERNAME,PASSWORD,DB): #1. Connect to the database and specify hostname, port, username, password, db #2. Open database #3. Manipulate the database to add content #4 to the database . Close database passdef Modify (hostname,port,username,password,db): #1. Connect to the database, and specify hostname, port, username, password, db #2. Open the database #3. Manipulate the database to add content #4 to the database . Close the database passdef remove ( HOSTNAME,PORT,USERNAME,PASSWORD,DB): #1. Connect to the database and specify hostname, port, username, password, db #2. Open Database # 3. Manipulate the database to add content #4 to the database . Close Database passdef Create (hostname,port,username,password,db): #1. Connect to the database, and specify hostname, port, username, password, db #2. Open the database #3. Manipulate the database to add content #4 to the database . Close the database pass # Add a list to the database create ([11,22,33])
With functional programming, hostname, ports, username, password, and DB are passed in and out every time. # # # #方式2 # package: Just change some of the variables encapsulated into the memory address # below we use the class to implement the database additions and deletions
Class Foo: #这个__init__ () is called a construction method, and execution # differs from functional programming when the class is instantiated: # #将hostname, port, username, password, DB encapsulated inside the self variable, # #当类里面的方法要用到这些参数时, we go directly to the self, without having to pass in the def __init__ (self,hostname,port,username,password,db) 1 parameters at a time: Self.hostname = hostname Self.port = port self.username = Username self.password = password self.db = db # def fetch (self): Pass def Remove (self): pass def Create (self): pass def Modify (self):
the entire process: 1. Create Foo class, ( 1. This step includes 3 steps: 1. Create Foo class 2. Create a constructor that will Continue to use the parameters encapsulated in the self inside; 3. Then create a series of methods )  2. Create a Obj1 object with Class Foo, and then instantiate the Foo class ( 1. This process consists of 2 steps: 1. Fields hostname, port, username, password, Db_name through foo (args), these fields are encapsulated in Obj1; 2. Then create a class object pointer, The class object pointer points to the in-memory Foo class ) 3. When the Obj.create (self) method is executed, the create in the Obj object is called (args ) method, ( 1. This process includes 2-step operation: 1. Locate the Foo class through the class object pointer in the Obj object and invoke the Create () method in the Foo class &NBSp  2. Return to the Obj object, remove the hostname, port, username, password, db_name fields in the Obj object 3. Upload these fields to the Create () method in the class and execute them )
Note:
1. Object only encapsulates fields and class object pointers
2. Using objects to execute methods in a class
2. Object-oriented inheritance
0. How to use inheritance, why use inheritance? #1. Method 1
Class Cat: Def Eat (self): Pass def Cry (self): pass def drink (self): pass def walk1 ( Self): passclass Dog (): def Eat (self): pass def Cry (self): pass def drink (self): Pass def walk2 (self): Pass
#方法2:
Class Animal: #基类 def Eat (self): pass def Cry (self): pass def drink (self): Pass Class Dog (animal): #派生类, inheriting the parent class animal def walk2 (self): Pass class Cat (animal): #派生类, inherit the parent class animal def Walk1 (self): Pass
1. Classic class and New class# #python其实开始只有函数式编程---> Then there are elementary classes and objects to program---> and then the Advanced class and object-oriented programming Classic class ****** * * * * * * * * * * Classic class:
Class C: Pass
New class:
Class C (object): pass or Class C (object): Pass class D (c): Pass
Note: python3.0 only supports modern classes, Java, C # can inherit only 1 classes, do not support multiple inheritance, and Python can inherit multiple classes "
2. The difference between a new class and a classic class in multiple inheritance (B, c both inherit D)1. In the definition, the new class defines the class that inherits object in the base class or its subclasses, whereas the classic class does not define inheriting object. 2. The new class performs multiple inheritance and uses a breadth-first strategy, whereas the classic class uses a depth-first strategy. (B, c all inherit D is the use of depth first)
So when do you use object-oriented programming? 1. Multiple methods use object-oriented programming when using the same variable or multiple variables, that is, encapsulating 2 in the constructor. Object-oriented programming is used when creating 1 kinds of things through a template # #类成员: is actually the method inside the class and the fields, properties, collectively referred to as the members of the class why the property is defined: The method is forged, Modified to 1 fields, #属性与方法的区别: 1. The property is defined with an adorner above it, which is @property 2. When the property is invoked without parentheses, that is, Obj.attribute 3. The property is a method of 1, but we modify the method, and then access the call in the form of a field -Type Access Example:
class Person (object): def __init__ (self): self.name = name Self.age = Age def func (self): #这是定义方法 return "123" @property def attribute: #这才是定义属性 return "123" obj = person (' jachy ', 21) Obj.attribute #调用属性obj. Func () #调用方法
* * * The difference between a static field and a normal field:1. Static fields are stored in the class. 2. Normal fields are stored in objects such as: Example:
class Person (object): name = "Jachy" #这种就是静态字段, saved in class def __init__ (self,name,age): self.name = name #这种就是普通字段, saved in the object self.age = Age obj = person ("Lilin", +) Obj.name #访问普通字段, accessed by object to Person.name # Access to static fields, accessed through classes
#类方法, static methods, common methods
Class Province (object): def __init__ (self): pass def-F1 (self): #定义普通方法, assigns the object obj to self when invoked Pass @classmethod #定义类方法: Decorate def f2 (CLS) with Classmethod: #参数用cls而不用self, the CLS full name is class, When invoked, assigns itself province to the CLS (only with CLS parameters) Pass
#类里面定义静态方法 ===== function
@staticmethod #定义静态方法, use @staticmethod to modify the Def f3 (ARG1,ARG2): #定义静态方法, the parameters can be arbitrarily written pass #普通方法的访问obj = Province () obj.f1 () #触发者是对象obj #类方法的访问Province. F2 () #触发者是类Province #静态方法的访问Province. f3 () # The trigger is the class province
Class methods and static methods (they do not need to create objects when they are accessed, their callers are classes, and normal method access is the object that must be created and whose callers are objects) 1. The class method can only pass 1 parameters 2. A static method is equivalent to a function
properties (Python is very familiar with very little, because it is not very well-used in the new class to use attributes)
Why do you have attributes? To forge and "modify" a method into 1 fields, there can be only 1 self arguments
2 ways to define a property: 1. Set properties by adorner; 2. Direct execution of the property (Func) methodDefining Properties: Method 1***************
Class Pager: def __init__ (self,current_page): self.current_page = current_page Self.per_items = ten @ Property def start: val = (current_page-1) * Self.per_items return val @property def end ( Self): val = self.current_page * Self.per_items return val
Figure 1: How to define a property 1########## #调用 ##################### #p = Pager (3) P.start #调用属性p. Stop #调用属性 "" "When you connect to a database, define the scope by using properties. SELECT * FROM A limit P.start:p.stop "" "******** Define Properties: Way 2************# #分2步: 1. Define 1 Methods Get_bar () 2. In defining 1 variables, such as Bar = Property (get_ bar), pass the pointer or address of the method to the property () to Class Foo:def Get_bar (self): return "jachy" bar = Property (Get_bar) #给get_bar Method defines the adorner #调用obj = Foo () result = obj. Barprint result Figure 2: How to define a property 2
What is the difference between parentheses after a class and parentheses after an object? 1. Parentheses after the object, such as obj (), means that the __call__ method in the corresponding class is called 2. Parentheses after the class, such as Foo ("Jachy"), that instantiate 1 classes Figure 1: Figure 2:
use of the __dict__ functionFigure 1. The object name. The use of __dict__ (used in writing the CMDB): It is actually the field inside the output object and saved as a dictionary Figure 2: class name. Use of __dict__: The field or method stored in the output class is actually
use of the __str__ functionFigure 1: When the __str__ method is not defined, the output obj object is actually the output obj object in memory address Figure 2: When the __str__ method is defined in the class, the output object obj is the return value of the __str__ method that invokes the corresponding class of the Obj object
Python Basics 5-object-oriented programming