1 Oop object Worldview-oop is the language of human cognition closest to real life
- All Business Objects
- The object has the law of motion and the internal state
- objects can be called to each other
2. Object-oriented
- Uniqueness: The object is unique and there are no two identical objects unless they are the same object
- Can be categorized: objects can be categorized
3.oop classification
- Packaging
- Inherited
- Polymorphic
Specific classes:
Structure
Classs Class Name:
Class Body
- Create an object using the class name (the __INIT__ function has a parameter list other than the first argument)
- When the object is created, the function is actually executed
__init__
, and __init__ does not create the object, but initializes the object
- When using an object to invoke a method, the first parameter is automatically passed in (self is the default, and the four-letter of this can be replaced by any character, but the position must be the first one)
Scope
- The immediate subordinate scope of a class is called a class variable
- Variables associated to an instance are called instance variables
Example:
1 class E: 2 ' E ' # the immediate subordinate scope of a class is called a class variable 3 4 def __init__ (self, name): 5 Self.name = name # the variable associated to the instance is called an instance variable
- Class variables are visible to classes and instances
- All instances share class variables
- Python can dynamically add and subtract attributes to an object, and when assigning a value to an instance's class variable, it is equivalent to dynamically adding an attribute to the instance, overriding the class variable
The lookup order of the properties
---------------------------------------------------need only objects. __dict__ To see how the content can be distinguished-----------------------------------------------------------------------------
Class adorners are typically used to add properties to a class--Methods are class-level
class method/static method
Methods are defined at the class level, but some methods use instance invocation, and some use classes to invoke
classI:def Print(self):Print('instance Method') @classmethod#when a method is Classmethod decorated, the first parameter becomes the class itself, and such a method is called a class method defClass_print (CLS):Print(ID (CLS))Print('class Method') @staticmethod#when a method is Staticmethod decorated, the first parameter is not passed automatically, and this method is called a static method . defstatic_print ():Print('static Method') defxxx_print ():Print('This is a function')
Instance methods can only be called by an instance
Class method: When a class method can be used by an instance and is used by an instance, the first parameter passed in is the class access control
classDoor:def __init__(self, Number, status): Self.number=Number self .__status= Status#double underline start, non-double bottom line ends are private, inaccessible outside the class defOpen (self):#MethodSelf.__status='opening' defClose (self): self.__status='closed' defstatus (self):returnSelf.__status def __set_number(self, number):#Double Slide First, non-double underline End method is also a private methodSelf.number = number
All double underscores start with a non-double-underlined member, all private members
Private members cannot be accessed outside the class
Python's private members are renamed by name, and the class name + attributes with double-down route are identified in the __dict__ (there is no real private member in Python, but this is negligible)
A variable that is labeled with a single underscore in some code can be interpreted as an internal variable, and you do not want to continue calling outside (in fact it can be called externally), and the interpreter does not do any processing
Starting with double underlines, the method that ends with a non-double underline is a private method.
If you use a @property decorator will turn a function with only the self parameter into a property, the value of the property, the return value of the method
class Door: def __init__ __status = status # Double underline, non-double down line ends are private, cannot be accessed outside the class @property # def status (self): return self. __status
D = Door ('opening')print(d.status) status is Door in the status method
Inheritance of Classes
- In parentheses after the class name is the inherited list, called the parent class or the base class or superclass-Python does not recommend multiple inheritance
- An obvious benefit of inheritance is the ability to get the properties and methods of the parent class, which can be confusing in deep succession.
- All the public can inherit
- Nothing private can inherit.
- What was it, inherited it or something?
- When a child class and a parent class have a member of the same name, a member of the subclass overrides the member of the same name in the parent class
Super (parent, self). Print () # Proxy method for the parent class of the type, and use the obj binding first parameter to specify the immediate parent class to call, and the second parameter specifies what to pass as the first parameter of the method when called
- When the parent class contains an initialization method with parameters, the subclass must require an initialization method, and the initialization method of the parent class is called in the initialization method
class Base: def __init__ (self, A, b): Self. __a = a self. __b = b def< /span> sum (self): return self.__a + self. __b
class Sub (Base): def __init__ (self, A, B, c): = c# self.__a = a# self.__b = b super (). __init__(A, B)
Super objects can only get properties of a class
Python Object-oriented OOP