Object-oriented Keywords: class, object, instance, method
Object-oriented three main features: encapsulation, inheritance, polymorphism
Class Foo: # defines the name def F1 (self): # F1 method Print ("success") obj = Foo () # Create Object Obj.f1 () # by Object execution method
Class and object relationships:
The self parameter is a python parameter that automatically passes the value, and the object executes the method.
Class Sqlhelp (): def __init__ (self, a1, A2, A3): # This method automatically executes the method that the object executes, and that is the print ("Auto-execute init") self.host = A1 self.name = a2 self.pwd = A3 def find (self): passobj1 = sqlhelp (' c1.salt.com ', ' DJC ', ' 100 ') # parameters correspond to A1,a2,a3print (obj1.host) respectively
>>>
Automatic execution
C1.salt.com
Encapsulates objects in an object
Class C1: def __init__ (self, Name, obj): self.name = name self.obj = objclass C2: def __init__ (self, Name, age): self.name = name Self.age = Age def show (self): print ("Mdzz") class C3: def __init __ (self, A1): self.aaa = a1c2_obj = C2 ("DJC", 19) # to write before C1 class C1_obj = C1 ("SB", c2_obj) ret = C1_obj.obj.nameprint (re T) >>>djcc3_obj = C3 (c1_obj) Ret2 = C3_obj.aaa.obj.show () print (Ret2) >>>mdzz
Simple single Inheritance
# inheritance is equivalent to copying a method of the parent class to a subclass, rather than going to the parent class to take
Class F1: # parent class, base class def show (self): print ("F1") def foo (self): print (Self.name) class F2 (F1): # subclass, Derived class def __init__ (self, name) self.name = Nameobj = F2 ("DJC") Obj.foo () >>>DJC
Complex single inheritance
Class F1: def F1 (self): self . F2 () # performs F2 's F2 def F2 (self): print ("F1,f2") class F2 (F1): def-F3 (self): self . F1 () # performs F1 's F1 def F2 (self): print ("f2,f2") obj = F2 () obj. F3 () # own method first, as a subclass do not ignore the inherited method of the parent class, if you do not have to go to the parent class to find >>>F2,F2
Simple Multiple inheritance
Class C1: def f2 (self): print ("C1") Class C2: def f2 (self): print ("C2") class C3 (C2,C1): # Left Priority def f3 (self): Passobj = C3 () obj.f2 () >>>c2
Slightly more complex multiple inheritance
Class C0: def f2 (self): print ("C0") class C1: def f2 (self): print ("C1") Class C2 (C0): def F1 ( Self): print ("C2") class C3 (C2,c1): def f3 (self): Passobj = C3 () obj.f2 () >>>c0
Complex multiple inheritance
Class c_2: def f2 (self): print ("c_2") class C_1 (c_2): def f2 (self): print ("C_1") class C0 (c_2): def F1 (self): print ("C0") class C1 (c_1): def F1 (self): print ("C1") Class C2 (C0): def F1 (self): Print ("C2") class C3 (C2,c1): def f3 (self): Passobj = C3 () obj.f2 () >>>c-1
Class h5: def Forever (self): Self.run () class H4 (H5): def __init__ (self): pass def run (self ): self.process () def process (self): print ("H4") class H3 (H4): def-__init__ (self): Passclass H2: def process (self): print ("H2") class A (H2, H3): passobj = A () obj.forever () >>>h2
Python object-oriented programming basics