Three main features of Python object-oriented

Source: Internet
Author: User

1. Package:

Encapsulation is the restriction of access to members of classes and objects, the manner in which they can be accessed, and how they cannot be accessed.

Classification:

Privatization Package: Current class/object species can be used, class/object outside and subclass/object are not available
Protected encapsulation: The current class/object and subclass/subclass object can use the door and cannot be used outside the class/object
Public package: Accessible from any location!

Cases:

classFather:#PropertiesSex ='male' Age= 48#Privatization Package #标志就是在属性或者方法前加两个下划线    __wife='beautiful white beauty long legs ~'    #protected Package #加一个下划线_money = 100#Method    def_say (self):Print(' protected Package')    def__eat (self):Print(' privatization package‘)
classSon (Father):
#protected members can be used here
defBuy (self):
    Print(' Dad has', Self._money,'Money')
#instantiate a Son object
s =Son ()
S.buy ()

2. Inheritance:

Inheritance is the ability to get member properties and member methods in another class. (Not all Members)

父类:用于被继承的类,称之为父类,也叫做基类,或者超类子类:继承其他类的类,称之为子类,也叫做派生类
Benefits of Inheritance
    • Build classes in your system to avoid repetitive operations.
    • The new class is often based on classes that already exist, which can increase the reusability of the code.
Characteristics of inheritance
    • The construction of the base class in inheritance (The init () method) is not automatically called, it needs to be called specifically in the construction of its derived class. different from C #
    • When calling a method of a base class, you need to prefix the class name of the base class with the Self argument variable. Unlike calling a normal function in a class, you do not need to take the self argument
    • Python always looks for a method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look up one by one in the base class. (Find the method that was called in this class before you can find it in the base class).

Classification:

  single inheritance: The way each class can inherit only one class is called single inheritance .

Format:

class Parent class:     Pass  class Subclass (Parent Class):# key steps for inheriting operations    Pass

Multiple inheritance: The way each class can inherit multiple classes at the same time is called multiple inheritance.

Format:

class Parent Class 1:     Pass class Parent Class 2:     Pass class Subclass (Parent Class 1, parent Class 2):     Pass

Diamond Inheritance:

Format:

class A:     Pass class B (A):     Pass class C (A):     Pass class D (b,c):     Pass     A   /   B   C   /    D

Problems with Diamonds:

  如果BC类同时继承了A类,D类又继承了BC两个类的情况下(菱形继承), 

  在调用BC中某个同名方法(该方法都继承自A类)时会导致继承自A类的该方法被多次调用。产生逻辑问题!

So Python uses the super () class to solve the multi-inheritance problem of diamond inheritance

Super (): Format: Super (). Method ()

Super is not a keyword, nor is there a function, he is a class

Super () does not look for the parent class, but instead looks for the previous class of the MRO list

Super () does not have any substantive relationship with the parent class, but it can sometimes be called to the parent class.

In the case of single inheritance, super () is always called the parent/Parent object

3. Polymorphism:

We have some methods in the parent class has been defined, but the subclass of our own use of time, found that, in fact, although we are all calculated wages, but the average employee's salary calculation method and the manager's calculation method is not the same, so this time, we can not directly call the parent class method of calculating wages. This time we need to use another object-oriented feature, polymorphic. We are going to re-implement the method of calculating the salary in the parent class within the subclass. Polymorphism contains overloads and overrides.

Here is not an example, on two pictures!

Rewrite

Rewriting is simply the way the class inherits from the Father's class, so that the same method in the parent class is overwritten, of course, you can still pass super. The Caculsalary method is used to call the parent class's payroll calculation method.

Overload

Overloading is the same method name in the class, the case of different parameters can be different form parameter type or parameter number, or the formal parameter order is different, but cannot make the return value type different.

Three main features of Python object-oriented

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.