Object-oriented programming--object oriented programming, short for OOP, is a programming idea. OOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data.
The object-oriented design idea is to abstract class, create instance according to class
Object-oriented abstraction is also higher than a function, because a class contains both data and methods to manipulate the data.
The class name is usually the first word in uppercase, with a camel-like
In Python, the variable name of an instance begins with __, and becomes a private variable (private) that can only be accessed internally and cannot be accessed externally.
This ensures that external code cannot arbitrarily modify the state inside the object, so that the code is more robust through access-restricted protection.
is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. __name cannot be accessed directly because the Python interpreter has changed the __name variable to _student__name, so it can still be
To access the __name variable via _student__name:
However, it is highly recommended that you do not do this because different versions of the Python interpreter may change the __name to a different variable name.
In Python, the variable name is like __xxx__, which starts with a double underscore, and ends with a double underscore, which is a special variable that can be accessed directly, not a private variable, so it cannot be used
Variable names such as __name__, __score__, and so on.
Sometimes you'll see an instance variable name that starts with an underscore, like _name, which means, "although I can be accessed, please treat me as a private variable and don't feel free to access it."
Python Multiple inheritance-new class, classic class
For example, there is a run method in the parent class, and the subclass defines the Run method, the parent class object. Run invokes the parent class's run, the subclass object. Run calls the subclass of Run, which is the polymorphic
The type of a subclass object is both a subclass and a parent class, which is also a polymorphic
The difference between static and dynamic languages: such as defining a function, passing in an object, calling the object's Run method
For a static language, such as Java, the parameter setting needs to specify the type, the incoming object must be of the specified type or subclass, or the call fails
For dynamic languages, the incoming object has no restriction of any kind, and only requires that the object has a run method.
This is the "duck type" of dynamic language, it does not require strict inheritance system, an object as long as "look like a duck, walk like a duck", it can be regarded as a duck.
Type (obj) indicates who created obj: When obj is instantiating an object, the result is a class, because obj is created by the class, and when obj is a class, the result is type, because the class is created with type
Isinstance (Obj,class) is the type that determines what obj is: When obj is instantiating an object, the result is a class, because the type of obj is the class, and when obj is a class, the result is object, because its type is Object
The use of type () is inconvenient for class inheritance relationships. To determine the class type, we can use the isinstance () function.
instance attribute belongs to each instance, and does not interfere with each other;
Class attributes are owned by the class, and all instances share an attribute;
Do not use the same name for instance properties and class properties, which will result in hard-to-find errors.
a class can act as a template, by defining a special __init__ method that, when creating an instance, forces some of the attributes that we think must be bound to be filled in. A function defined in a class is only a bit different than a normal function, which is that the first argument is always the instance variable self and, when called, does not pass the argument. method is the function that binds to the instance, and the normal function is different, the method can directly access the data of the instance;classFoo:#Defining ClassesStaticword ='Staticword'#static Field/class properties, which can be accessed by classes/objects __nationl=' China'#a static private field that can be used inside a class with the class name. xxxx/object. xxxx access, external access should be accessed indirectly through methods def __init__(self,name,age):#class, which is automatically executed when an object is createdSelf.name = Name#define normal/Normal variables, access by objectSelf.__age= Age#private fields/private variables that can be accessed through objects within the class and cannot be accessed directly outside the #private field of parent class, cannot inherit, method of subclass cannot call private field of parent class directly defF1 (self):#defines the normal method, object. xxxx () to access/class name. F1 (object) can also be accessed Print('Perform common methods') @staticmethoddefF2 ():#defines a static method, the class name. XXXX ()/object. XXXX () can be accessed Print('executing a static method') @classmethoddefF3 (CLS):#defines the class method, the class name. XXXX ()/object. XXXX () can be accessed Print('Execute class Method') @property#Nondescript 1 defF4 (self):#object. XXXX Direct access/But class name. xxxx access is not successful, also does not error? Print(' Property') return1@f4. Setter#Nondescript 1 defF5 (self,xxx):#object. f5 = 123 (any numeric value), where the method name and definition are F4 Print('Perr.setter') @f4. Deleter#Nondescript 1 defF6 (self):#del object. f6, where the method name and definition are F4 Print('Perr.deleter') defwordtest (self):Print(self.)__age)#indirect access to private fields Print(foo.)__nationl)#indirect access to private fields returnFoo.__nationl#indirect access to private fields def __call__(Self, *args, **kwargs):#object ()/class name () () Automatic execution Print('__call__') def __int__(self):#Int (object) is automatically executed and the return value is assigned to the Int object Print('__int__')#does not seem to be assigned to an object, because after an int (object) operation, the object is printed separately, showing the ' s ' 123 ' return1233#must be a number, otherwise error, def __str__(self):#Automatic execution of Str (object) Print('__str__')#Print (object) also automatically calls the object's Str method #Print (object) ==print (str (object)) return 's ' 123' def __add__(Self, Other):#The object + object defines the method, executes the + when the first object is automatically executed, the second object is passed in as a parameter, and the custom value is returned returnself.name+Other.namedef __del__(self):#automatic execution of this method when an object is destroyed Print('destructor Method')#it's no use. #object. __dict__ returns all content encapsulated in the object as a dictionary #The class name. __dict__ returns all content in the class as a dictionary, including a note ("") as a member of a class and a pair of key-value pairs def __getitem__(Self, item):#Object [9] automatically executes the method, passing 9 as a parameter to the item #In Python3, List[1:6:2] or execute __getitem__ in the same way as a list slice, but not in Python2 ifType (item) = =Slice:Print('object is slice, slice processing') Print(item.start,111) Print(item.stop,222) Print(item.step,333) Else: Print('object is non-slice, index processing') #return item+10 def __setitem__(Self, key, value):#Object [9] = 123; Automatic execution of this method, no need to set returnself.key=valuePrint('__setitem_____') def __delitem__(self, key):Print('delete%s'%key)#del Object [9]; Automatically executes the method without setting a return def __iter__(self):returnITER ([]) #If there is a __iter__ method in the class, the class produces an object that is---iterate over an object #The return value of the object. __iter__ (): Iterator #The For loop object is an iterator that executes directly next #The For Loop object is an object that can be iterated when an object is executed. __ITER__ (), returns an iterator, and then executes next on the iteratorfo= Foo ('Allen', 30) forIinchHot#1, if the FO is an iterator, the direct iteration; 2, if Fo is an iterative object, execute the __iter__ method, take the return value Print(i,112233)
class Properties, Methods
classF:defF1 (self):Print('111')classS (F):#inherit F defF2 (self):Print("222") defF1 (self):#If you do not want to inherit the method of the parent class, just create a corresponding method inside yourselfSuper (S,self). F1 ()#if you want to execute a function with the same name as the parent class, you can Print('333')#super (s,self) equals the parent class #f.f1 () #另一种调用父类方法的写法obj= S ()#2 actions, 1. Creating an object, 2. Performing a __init__ property assignmentobj.f1 () obj.f2 () s.f2 (obj)#another way to invoke a methodone of the three main characteristics of object-oriented: Encapsulation of object-oriented three major characteristics of the second: inheritance1, inheritance2, overriding prevents methods in the parent class from being executed#Modify the method of the parent class3, self is always the caller who executes the method4, super (subclass, Self). Methods in the parent class (...) Parent class name. Method in parent class (self,...) 5. Support multiple inheritance in Python#Java cannot inherit moreA. Left Priority B. One way to the black c. At the same root, the root finally executes three characteristics of object-oriented: Polymorphism====>native Polymorphic class Members:#Field-normal field, saved in object, accessible only by object-static fields, which are stored in a class, can be accessed through objects, or accessed through a class#Method-Normal method, saved in class, called by object, self="Object-static methods, which are saved in the class, are called directly by the class-Class method, saved in the class, called directly by the class, cls="Current class#Properties, Attributes-Nondescript
Notes
python-Object-oriented