python - three features of object-oriented (inheritance, polymorphism, encapsulation)

Keywords: PHP Python

I. succession.

1. Inheritance: save code, standardize code

2. The inherited class is called the parent class, the base class, and the inherited class is called the derived class and the child class.

3. Category: divided into new category and classic category.

New class: classes that inherit the object class are called new classes. In Python 3, all classes by default inherit the object class, so they are all new classes.

Classic class: a class that does not inherit an object class is called a classic class. In python2, all classes by default are classic classes. You can add an object class to it to become a new class.

class A:    # A Is the parent class, the base class, B Is a derived class, subclass
    pass

class B(object A): # Add to object Class, into a new class
    pass    

4. Inheritance: single inheritance and multiple inheritance

5. Query order

Single inheritance: the query order of new and classic classes is the same

Multiple inheritance:

Classic class: in case of multiple inheritance, depth first search is adopted

New class: in case of multiple inheritance, breadth first search is adopted

Depth first, breadth first: only two classes can be inherited

Depth first: one way to the dark

Breadth first: when one road reaches the penultimate level, judge that if other roads can reach the end point, take another road, otherwise, go to the end point.

6. The method of executing this class and the parent class

class Animal:
    def __init__(self, name, sex, age):
        self.name = name
        self.sex = sex
        self.age = age

    def eat(self,a1):
        print('%s eat%s' % (self.name, a1))
# The method of executing this class and the parent class
class Bird(Animal):
    def __init__(self,name,sex,age,wing):
        # Animal.__init__(self,name,sex,age)    # Parent class name. Method name (parameter)
        super().__init__(name,sex,age)  # super().Method name(parameter(self Automatic passing value))
        self.wing = wing

Two. Polymorphism.

# python:Is a weakly typed language
# Polymorphism: python There is polymorphism everywhere
# python There is no polymorphism, no matter what type, the incoming function, or the encapsulation into the object.
# python It's duck type
# These classes, methods with the same function, are collectively referred to as duck types
class Str:
    def index(self):
        pass


class List:
    def index(self):
        pass


class Tuple:
    def index(self):
        pass

Three. Packaging

# python One of the three characteristics of: Encapsulation
# Private static field
# class B:
#     __money = 1000
# class A(B):
#     name = 'alex'
#     __age = 18
#     def func(self):
#         print('It's sunny today')
#         print(a1.__age)
# a1 = A()
# print(a1.name)
# print(a1.__age) # Instantiated object cannot access private static field
# print(A.__age) # Class name cannot access private static fields
# print(a1.__money)
# For private static fields, the external of the class cannot be accessed

# For private static fields, the inner part of the class can be accessed
# a1.func()

# For private static fields, the inner part of the class can be accessed, and neither the outer part nor the derived class can be accessed

# print(A.__dict__)
# For private static fields, once the method is executed, it will be generated automatically._Class name_Private static field, you can instantiate the object._Class name__Private static field access
# print(a1._A__age)

# Private static method

# class A:
#     def __sport(self):
#         print('Run')
#     def sport1(self):
#         print('Swimming')
#         self.__sport()
# a1 = A()
# a1.__sport() # Instantiated object cannot access private method
# A.__sport(11)   # Class name cannot access private method
# a1.sport1() # Private methods can be accessed inside a class
# print(A.__dict__)

IV. interface class, abstract class

# python The concept of no interface
# python Interface class, abstract class: make a rule
from abc import ABCMeta,abstractmethod
class Payment(metaclass=ABCMeta): # Abstract class
    @abstractmethod
    def pay(self):pass # A code has been established
class Alipay(Payment):
    def __init__(self,money):
        self.money = money

    def pay(self):
        print('You paid for it with Alipay.%s element' % self.money)

class Jdpay(Payment):
    def __init__(self,money):
        self.money = money

    def pay(self):
        print('You paid with Jingdong%s element' % self.money)
class Wechatpay(Payment):
    def __init__(self,money):
        self.money = money
    def weixinpay(self):
        print('You paid by wechat%s' % self.money)
def pay(obj):
    obj.pay()   # Normalized design
    
a1 = Alipay(100)
a1.pay()
w1 = Wechatpay(100)
w1.weixinpay()  # Wrong, no pay()Method

Posted by Ben5on on Tue, 15 Oct 2019 12:02:51 -0700