1.#class #A class is a template that can contain multiple functions and functions in a template . #define a class classBar:deffoo (SELF,AGR):Print(SELF,AGR) obj=Bar ()Print(obj)Print(Obj.foo ("methods to invoke a class"))2.#Method #writing a function in a class is a method. #Construction Method #Common Methods classPerson :def __init__(self,name,age):#constructs the attribute of the method, after invoking the class name (), automatically executes the construction method #Self who calls the class, who is selfSELF.N =name SELF.A=Age Self.xue=0defShow (self):#Common Methods Print("%s--%s"%(SELF.N,SELF.A)) obj= Person ("haha", 20) obj.show ()3.#one of the three main features of object-oriented: encapsulation classBar:def __init__(self,n,a): Self.name=N self.age=a Self.xun=0 B1= Bar ("Zhangw", 11) B2= Bar ("haha", 31)4.#two main characteristics of object-oriented: Inheritance #Self is always the caller who executes the method #Inheritance classParent class:Pass classChild Class (parent class):Pass #subclasses can call methods of the parent class #if the subclass has the same method name as the parent class, it takes precedence to invoke its own, which is overriding #override to prevent methods from executing the parent class #after overriding, if you want to execute a method of the parent class1.#Super (subclass, Self). Methods in the parent class ()2.#Parent class name, method in parent class (self,... ) #multiple inheritance support in Python #Principles #1. Left is preferred, here the left refers to the time of the call, such as: Class Zilei (FULEI1,FULEI2), Fulei1 priority #2. Find the right side after finding the left side #3. If the left and right sides converge to the same root at the end, find the root5.#Three main characteristics of object-oriented: Polymorphism #native polymorphic, "duck type" #when you see a bird walking like a duck, swimming like a duck, and barking like a duck, the bird can be called a duck . classF1:Pass classS1 (F1):defShow (self):Print(s1.show)classS2 (F1):defShow (self):Print(s2.show)defFunc (obj):Print(Obj.show ()) S1_obj=S1 () Func (s1_obj) s2_obj=S2 () Func (s2_obj)6.#class member #Field #normal field, saved in object, accessible only by object #static fields, saved in a class, can be used in classes, or object access #Method #normal method, saved in class, called directly by object, self = > object #static methods, which are saved in the class, are called directly by the class #class method, saved in class, called directly by the class CLS = > Current Class classProvince:country="China" def __init__(self,name): Self.name=name @staticmethod#Static Methods defJingtai ():Print("I'm a static method and I don't need an object") @classmethod#class Method defCLASSMD (CLS):Print(CLS)Print("haha") Print(province.country) Hebei= Province ("Hebei") Print(Hebei.name) Province.jingtai () province.classmd ()#Properties, Attributes #@property #the definition and invocation of a property is a few points to note: #when defining, add @property adorners on the basis of common methods; #when defined, the property has only one self parameter #when called, no parentheses are required #method: Foo_obj.func () #Property: Foo_obj.prop ################ defining Attributes ############### classGoods (object): @propertydefPrice (self):Print(property) @price. SetterdefPrice (Self, val):Print(Val) @price. DeleterdefPrice (self):Print(666) ################ Call ############### #obj = Goods () #Obj.price # Automatically executes the @property decorated price method and gets the return value of the method #obj.price = 123 # automatically executes @price. Setter-Modified price method, and assigns 123 to the parameters of the method #del obj.price # Automatic execution @price. Deleter-Modified Price Method
[Python Object-oriented]--basic article