Python development-object-oriented inheritance and derivation

Source: Internet
Author: User

1. Inheritance
What is inheritance?
Inheritance is a way of creating a new class, called a subclass or derived class
The parent class is also referred to as the base class, Super class

Subclasses can "inherit" the attributes of the parent class, which can reduce code redundancy

How do I find an inheritance relationship?
The first is to abstract, then inherit, the inheritance describes a parent-child relationship/affiliation

inherited syntax
Class Foo1:
Pass
Class Foo2:
Pass

Class Bar (Foo1,foo2):
Pass

Class Bar: # does not inherit classes from any class in Python3, Inherits object by default
Pass

Derived
Class Parent:
X=1
def F1 (self):
Pass

Class Sub (Parent):
def f2 (self):
Pass
def F1 (self):
Pass

New classes and Classic classes:
New class: Inherits the subclass of the object class, as well as the subclass of the child class ...
In the Python3, all the new classes.

Classic class: A subclass that does not inherit object, and a subclass of that subclass ...
Only classic classes are available in Python2, and subclasses of any class that do not inherit in Python2 do not
Inherit the object class by default
Class Foo (object):
Pass


The difference between a classic class and a new class: On Diamond Inheritance
Classic class: Depth-First lookup
New class: Breadth First search
Foo.mro ()

Reuse the functionality of the parent class in the method defined by the subclass:
Mode one: unrelated to inheritance
A function that specifies the name of a class to invoke:
Class name. function name (object, parameter 1, parameter 2,...)

Way two: Strictly dependent on inheritance
Super (own class name, self). Binding method (parameter 1, parameter 2, ...) )


Combination
Role:
Combinations are the same as inheritance, and are used to reduce the duplication of code between classes and classes

Defined:
An object of Class A has a property with the value of the Class B object
The combination of Class A and Class B is based on this approach
Object can use both data and functionality in Class A, as well as data and functionality in Class B


2. Encapsulation
What is encapsulation?
Encapsulation (literally understood) is hidden, hiding refers to hiding an attribute inside a class.
Allows users outside of the class to be used directly. In the PY, it is the beginning of the __ to hide a property.

Supplemental Note: Encapsulation is definitely not a pure meaning of concealment
The purpose of defining the attribute is to let the user use it, and the user wants to use the property hidden within the class.
The designer of the class needs to open an interface within the class (define a method) to access the hidden properties inside the method
, the user will then use this method to "indirectly" access internally hidden properties
As a designer of a class, you can attach arbitrary logic on top of an interface to strictly control the operation of a class's consumer property.


class people:
def __init__ (self,name):
Self.__name=name

def tell_name (self):
# Add Logic
Return Self.__name

Obj=people (' Egon ')
#obj. __name
Obj.tell_name ()

Encapsulation Function Properties: Isolation complexity
Class ATM:
def __card (self):
Print (' Insert card ')
def __auth (self):
Print (' User authentication ')
def __input (self):
Print (' Enter withdrawal amount ')
def __print_bill (self):
Print (' Printed bill ')
def __take_money (self):
Print (' withdrawals ')

def withdraw (self):
Self.__card ()
Self.__auth ()
Self.__input ()
Self.__print_bill ()
Self.__take_money ()

Python development-object-oriented inheritance and derivation

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.