Python Object-oriented
First, the concept of distinction:
- Process oriented: Write base code from top to bottom according to business logic
- Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
- Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."
Ii. Object-oriented programming (object Oriented Programming,oop, OO programming)
Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".
A class is a template that can contain multiple functions and functions in a template.
Objects are instances created from templates that can execute functions in a class through an instance object
Writing specification:
Class name (object):
#定义构造函数
def __init__ (self):
Pass
#定义方法 (function)
def sayhi (self):
Print ("Hello")
- Class is a keyword that indicates that classes
- Create the object, and then add parentheses to the class name
- Functions in a class The first argument must be self
- A function defined in a class is called a method.
Third, function
__init__ () method
__init__ () is a special approach (special method). Python will have some special methods, and Python will handle them in a special way. The name of the special method is characterized by a two underscore before and after.
The special of the __init__ () method is that if you define this method in a class, Python automatically calls this method (which is also called initialization) once you have created the object from that class.
Object-oriented three major features
The three major characteristics of object-oriented are: encapsulation, inheritance and polymorphism.
first, the package
Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.
Therefore, when using object-oriented encapsulation features, you need:
- Encapsulate content somewhere
- To invoke the encapsulated content from somewhere
First step: encapsulate the content somewhere
Step two: Call the encapsulated content from somewhere
When the encapsulated content is called, there are two cases
- Called directly through an object
- Indirectly called through self
1. Direct invocation of encapsulated content by object
2. Indirectly invoking the encapsulated content via self
In summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content.
Ii. Inheritance
Inheritance, the inheritance in object-oriented is the same as the inheritance in real life, that is, the child can inherit the parent's content.
For example:
Cats can: Meow meow, eat, drink, pull, sprinkle
Dogs can: bark, eat, drink, pull, sprinkle
If we were to create a class for both cats and dogs, we would need to do all of their functions for cats and dogs.
Animals: Eating, drinking, pulling, spreading
Cat: Meow Meow (cat inherits function of animal)
Dog: Barking (dogs inherit the function of animals)
Therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, and the subclass inherits only the parent class without having to implement each method in one.
Note: In addition to the names of subclasses and parent classes, you may have seen derived and base classes that are only different from subclasses and parent classes.
- Whether multiple classes can be inherited
- If you have inherited multiple classes that have the same function in each class, then that one will be used?
1. Python classes can inherit multiple classes, and Java and C # can inherit only one class
2. If the Python class inherits more than one class, there are two ways to find the method: Depth first and breadth First
- When a class is a classic class, in the case of multiple inheritance, the
- When a class is a new class, in multiple inheritance cases, the
is found in the breadth-first way
Classic class and new class, literally can see an old a new, new inevitably contain with many functions, is also recommended after the wording, from the wording of the words, if the current class or the parent class inherits the object class , then the class is a new class, otherwise it is the classic class.
Three, polymorphic
Pyhon does not support polymorphism and is not polymorphic, the concept of polymorphism is used in strongly typed languages such as Java and C #, while Python advocates "duck type".
With regard to Python polymorphism, like JavaScript, direct access to the properties of an object without the need for an interface, no type conversion.
For the type of judgment, there are the type () function of the grab, and the isinstance () function determines whether the subclass of a function.
Summarize
The above is the introduction of object-oriented primary knowledge in this section, summarized as follows:
- Object-oriented is a programmatic approach that is implemented based on the use of classes and objects
- Class is a template that wraps multiple "functions" in a template for use
- Object, an instance created from a template (that is, an object) used to invoke a function that is wrapped in a class
- Object-oriented three major features: encapsulation, inheritance, and polymorphism
Primary knowledge object-oriented-python