First, create a class
Syntax format:
class class Name: Pass
To create a method:
1 Construction methods
1 class Person : 2 3 def __init__ (self, name,age): 4 5 # Construct method , construct method attribute, class name () automatically execute construction method 6 7 SELF.N = name8 SELF.A = Age
To perform a construction method:
1 obj = person (' li Huan ', 18)
2 Common methods:
1 class Person : 2 3 def Show (self): 4 Pass
To perform a common method:
1 obj = Person ()2 obj.show ()
Second, the package
self refers to obj, which refers to the caller of the Calling method
1 classBar:2 def __init__(self, n,a):3Self.name =N4Self.age =a5Self.xue ='o'6 7Obj1 = Bar ('Alex', 123)8 9Obj2 = Bar ('Eric', 456)
Iii. inheritance
Inheritance format:
1 class Parent class: #基类 2 Pass 3 4 class Subclass (Parent Class): #派生类 5 Pass
After overriding the parent class method, you want to execute the parent class method and the overridden method.
Method one: Super (subclass, Self). Methods in the parent class (...)
Method Two: The parent class name. Methods in parent class (self,...)
1 classF:2 defEat (self):3 Print('Eating')4 5 defSleep (self):6 Print('F is sleeping')7 8 classs (f): # Add the parent class to inherit in parentheses 9 defSleep (self): # overriding parent class methods TenSuper (S,self). Sleep ()#Method One One Print('S is sleeping') AF.sleep (self)#Method Two - -obj =s () the Obj.sleep () -Obj.eat ()
Support Multiple inheritance
Multiple inheritance Features:
A. Left priority
B. A road goes to the black
C. At the same root, the root is finally executed
1 classbasereuqest:2 3 def __init__(self):4 Print('Basereuqest.init')5 6 7 classRequestHandler (basereuqest):8 def __init__(self):9 Print('Requesthandler.init')TenBasereuqest.__init__(self) One A defServe_forever (self): - #Self , it's obj. - Print('Requesthandler.serve_forever') the self.process_request () - - defprocess_request (self): - Print('requesthandler.process_request') + - classMinx: + A defprocess_request (self): at Print('minx.process_request') - - - classSon (Minx, RequestHandler): #按照从左到右, a line goes to the end, if there is the same root, then the root is finally executed - Pass - in -obj = Son ()#Init toObj.serve_forever ()
When obj = Son is instantiated, the Init method is automatically executed first, there is no init in the son class, the Init method is found in the first parent class, there is no init in Minx, and the Init method is found in the second parent class, after which the Init method is executed, and then called Server_ Forever () Common method.
Iv. members of the class
Field
- Static fields
Static fields are stored in classes, accessed through objects, or through classes.
- Normal field
Normal fields are saved in the object and can only be accessed through the object.
Cases:
1 classProvince:2Country =' China' #static fields3 def __init__(self,name):4Self.name = Name#normal field5 6obj = Province ('Beijing')7 Print(Obj.name)8 Print(Province.country)
Method
- Common methods
The normal method is saved in the class and called by the object. Self represents the object
- Static methods
Static methods are saved in the class and are called directly by the class
- Class method
Class methods are stored in the class, called directly by the class, and the CLS refers to the current class
Cases:
1 classfoo:2 3 defBar (self):#normal method, self is the object4 Print('OK')5 6@staticmethod#Static Methods7 defSTA ():8 Print('123')9 Ten@staticmethod#Static Methods One defStat (A, b): A Print(A, b) - -@classmethod#class Method the defaaa (CLS): #CLS is a class name - Print('class Method') - -obj =foo () + Obj.bar () - Foo.sta () +Foo.stat () AFOO.AAA ()
Nondescript--Attribute
Access the corresponding method in another form by adding an adorner.
1 classfoo:2 @property #用于执行obj. Bar3 defBar (self):4 Print('1213')5 return6666 7 @bar. Setter #用于执行obj. Bar = 1238 defBar (self,val):9 Print(val)Ten One @bar. deleter #通过del Obj.bar can locate and access the following methods A defBar (self): - Print(999) - theobj =foo () - Obj.bar -Obj.bar = 123 - delObj.bar
Instance:
Implementing paging
1 classpagenation:2 3 def __init__(self,current_page):4 5 Try:6p =Int (current_page)7 exceptException as E:8p = 19 TenSelf.page =P One A @property - defStart (self): -val = (self.page-1) * 10 the returnVal - - @property - defEnd (self): +val = self.page * 10 - returnVal + ALi = [] at forIinchRange (5000): - li.append (i) - - whileTrue: -page = input ('>>>') -obj =pagenation (page) in #print (Li[obj.start (): Obj.end ()]) - Print(Li[obj.start:obj.end])#after adding adorners to the method, you can directly use the object name. Method name to access the method.
python--Object-oriented