Python-object-oriented: three major features & advanced features


foreword

process oriented: (Procedure Oriented) is a process-centric programming idea. Analyze the steps needed to solve the problem, and then use functions to implement these steps step by step, and call them one by one when using them.
functional programming: It is a programming method that treats computer operations as calculations of functions. The most important foundation of functional programming language is lambda calculus (lambda calculus), and the function of lambda calculus can accept functions as input (parameter) and output (return value).
Main idea: Try to write the operation process as a series of nested function calls.
object oriented: It is based on the systematic way of thinking that people understand the objective world, and decomposes the constitutive problem into various objects. The purpose of establishing an object is not to complete a step, but to describe the behavior of a certain thing in the entire problem-solving step.


1. Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

1. Objects and classes

Class (Class): It is the reflection of entities in the real or thinking world in the computer, which encapsulates data and operations on these data.
Object (Object): It is a variable with class type. Classes and objects are the most basic concepts in object-oriented programming techniques.
The difference between classes and objectsIt's the difference between a fish and a salmon; it's the difference between a cat and a blue cat.

Class (Class) is a template for creating instances
Object (Object) is a specific instance

insert image description here

insert image description here

2. Encapsulation

insert image description here

  • The construction method __init__ differs from other ordinary methods in that when aAfter the object is created, will immediatelycall
    constructor
    . Automatically execute the contents of the constructor.
  • For object-oriented encapsulation, it is actually usingConstruction methodwrap the content inobject, and then
    obtain the encapsulated content directly or indirectly through the object.

insert image description here
Consolidated package features:

"""
创建一个类People,
拥有的属性为姓名, 性别和年龄,
拥有的方法为购物,玩游戏,学习;

实例化 对象,执行相应的方法。

显示如下:
小明,18岁,男,去西安赛格购物广场购物
小王,22岁,男,去西安赛格购物广场购物
小红,10岁,女,在西部开源学习

提示:
属性:name,age,gender
方法:shopping(), playGame(), learning()

"""

class People:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    def shopping(self):
        print(f'{self.name},{self.age}岁,{self.gender},去西安赛格购物广场购物 ')

    def learning(self):
        print(f'{self.name},{self.age}岁,{self.gender},在西部开源学习')

p1 = People('小明', 18, '男')
p2 = People('小王', 22, '男')
p3 = People('小红', 10, '女')

p1.shopping()
p2.shopping()
p3.learning()

3. Inheritance

Inheritance describes the relationship between things. When we define a class, it can inherit from an existing class. The new class is calledsubclass, extension class(Subclass), and the inherited class is calledbase class, superclass, or superclass(Baseclass、Superclass)。

  • 1. How to let the implementation inherit?
    When the subclass inherits, when defining the class, the parenthesis () is the name of the parent class
  • 2. What is the working mechanism of inheritance?
    The properties and methods of the parent class will be inherited to the subclass. An example is as follows: If the subclass does not define the __init__ method, but the parent class does, then
    this method will be inherited when the subclass inherits the parent class, so as long as the object is created, the inherited
    __init__ method will be executed by default

(1) Override the parent class method

Override the parent class method: that is, in the subclass, there is a method with the same name as the parent class, and the method in the subclass will override the method with the same name in the parent class
insert image description here
insert image description here

class Student:
    """父类Student"""
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def learning(self):
        print(f'{self.name}正在学习')

    def choice_course(self):
        print('正在选课中'.center(50, '*'))

class MathStudent(Student):
    """MathStudent的父类是Student"""
    def choice_course(self):
        # 需求: 先执行父类的choice_course方法, 在个性化执行自己的方法。
        # Student.choice_course(self)  # 解决方法1: 直接执行父类的方法,但不建议
        # 解决方法2: 通过super找到父类,再执行方法(建议且生产环境代码常用的方式)
        super(MathStudent, self).choice_course()
        info = """
                    课程表
            1. 高等数学
            2. 线性代数
            3. 概率论
        """
        print(info)

# 实例化
m1 = MathStudent("粉条博士", 8)
m1.choice_course()

s1 = Student("粉条博士", 8)
s1.choice_course()

(2) Multiple inheritance

Multiple inheritance, that is, subclasses have multiple parent classes and have their characteristics

insert image description here

insert image description here

insert image description here

"""
新式类: 广度优先算法
经典类: 深度优先算法(py2中的部分类属于经典类)

python3所有的类都属于新式类。新式类的继承算法是广度优先。

# 分析多继承的相关代码
>pip install djangorestframework
from rest_framework import viewsets
viewsets.ModelViewSet
"""

class D(object):
    def hello(self):
        print('D')
class C(D):
    # def hello(self):
    #     print('C')
    pass
class B(D):
    pass
    # def hello(self):
    #     print('B')
class A(B, C):
    pass
    # def hello(self):
    #     print('A')
a = A()
a.hello()

(3) Private properties and private methods

In Python, if the variable name of the instance starts with __, it becomes a private variable/property (private), and if the function name of the instance starts with __, it becomes a private function/method (private), which can only be accessed internally, not externally.

insert image description here

insert image description here

4. Polymorphism

Polymorphism (Polymorphism) literally means "multiple states". In object-oriented languages, multiple different implementations of interfaces are known as polymorphism. In layman's terms: The same operation acts on different objects, can have different interpretations, and produce different execution results

The advantage of polymorphism is that when we need to pass in more subclasses, we only need to inherit the parent class, and the method can either not be rewritten directly (that is, use the parent class), or a unique one can be rewritten. This is what polymorphism means. The caller just calls, regardless of the details, and when we add a new subclass, we only need to ensure that the new method is written correctly, regardless of the original code. This is the famous "open and close" principle

Second, the application of the three characteristics

1. Encapsulation of linked list

"""
参考链接 https://www.cnblogs.com/klyjb/p/11237361.html
数组: 需要连续的内存空间
链表: 不需要连续的内存空间
                数组              链表
增加元素        O(n)                O(1)
删除元素        O(n)                O(1)
修改元素        O(1)                O(n)
查看元素        O(1)                O(n)
"""

# 封装节点类
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

    def travel(self, head):
        """遍历链表里面的每一个元素"""
        while head:
            print(head.val, end=',')
            head = head.next

def create_l1():
    # l1 = 2,4,3
    # l2 = 5, 6, 4
    l1 = ListNode()
    node1 = ListNode(val=2)
    node2 = ListNode(val=4)
    node3 = ListNode(val=3)
    l1.next = node1
    node1.next = node2
    node2.next = node3
    return  l1.next

def create_l2():
    # l1 = 2,4,3
    # l2 = 5, 6, 4
    l2 = ListNode()
    node1 = ListNode(val=5)
    node2 = ListNode(val=6)
    node3 = ListNode(val=4)
    l2.next = node1
    node1.next = node2
    node2.next = node3
    return  l2.next

def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
    res = 0
    l3 = ListNode()
    cur = l3
    while(l1 or l2):
        if(l1):
            res += l1.val  # res=2
            l1 = l1.next
        if(l2):
            res += l2.val # res=2+5=7
            l2 = l2.next
        # res=10, val=0, res=>val val=res%10
        # res=14, val=4, 14%10=4
        l3.next = ListNode(res%10)
        l3 = l3.next
        # res=10, 进位为1, 10//10=1
        # res=14, 进位为1, 14//10=1
        res  //= 10
    if res == 1:
        l3.next = ListNode(1)
    return cur.next

insert image description here

insert image description here

insert image description here

2. Stack encapsulation

class Stack(object):
    """栈的封装[1, 2, 3, 4]"""

    def __init__(self):
        self.stack = []

    def push(self, value):
        """入栈"""
        self.stack.append(value)
        print(f"入栈元素为{value}")

    def pop(self):
        """出栈"""
        if self.is_empty():
            raise  Exception("栈为空")
        item = self.stack.pop()
        print(f"出栈元素为{item}")
        return  item

    def is_empty(self):
        """判断栈是否为空"""
        return  len(self.stack) == 0

    def top(self):
        """返回栈顶元素"""
        if self.is_empty():
            raise  Exception("栈为空")
        return  self.stack[-1]

    def __len__(self):
        """魔术方法, len(object)自动执行的方法"""
        return  len(self.stack)

if __name__ == '__main__':
    stack = Stack()
    stack.push(1)
    stack.push(2)
    stack.push(3)
    print(len(stack))  # 3
    stack.pop()
    print(stack.is_empty()) # False
    print(stack.top())  # 2

3. Queue encapsulation

class Queue(object):
    """
    队列的封装
    1. 列表的左侧队尾
    2. 列表的右侧队头
    """
    def __init__(self):
        self.queue = []

    def enqueue(self, value):
        """入队"""
        self.queue.insert(0, value)
        print("入队元素为:", value)

    def dequeue(self):
        """出队"""
        if self.is_empty():
            raise  Exception("队列为空")
        item = self.queue.pop()
        print("出队元素:", item)
        return  item

    def __len__(self):
        """获取队列的长度"""
        return  len(self.queue)

    def first(self):
        """获取队头元素"""
        if self.is_empty():
            raise Exception("队列为空")
        return  self.queue[-1]


    def last(self):
        """获取队尾元素"""
        if self.is_empty():
            raise Exception("队列为空")
        return  self.queue[0]

    def is_empty(self):
        """判断队列是否为空"""
        return  len(self.queue) == 0


if __name__ == '__main__':
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    queue.enqueue(3)
    print(queue.is_empty()) # False
    queue.dequeue()  # 1出队, 队列只剩32
    print(queue.first())  # 2
    print(queue.last())  # 3

4. Encapsulation of binary tree

"""
二叉树:
    https://www.cnblogs.com/polly333/p/4740355.html
"""

class Node(object):
    """节点类"""
    def __init__(self, val=None, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class BinaryTree(object):
    """封装二叉树"""
    def __init__(self, root):
        self.root = root

    def pre_travel(self, root):
        """先序遍历: 根左右"""
        if (root != None):
            print(root.val)
            self.pre_travel(root.left)
            self.pre_travel(root.right)

    def in_travel(self, root):
        """中序遍历: 左根右"""
        if (root != None):
            self.in_travel(root.left)
            print(root.val)
            self.in_travel(root.right)

    def last_travel(self, root):
        """后序遍历: 左右根"""
        if (root != None):
            self.last_travel(root.left)
            self.last_travel(root.right)
            print(root.val)

if __name__ == '__main__':
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node7 = Node(7)
    node8 = Node(8)
    node9 = Node(9)
    node10 = Node(10)

    bt = BinaryTree(root=node1)
    node1.left = node2
    node1.right = node3
    node2.left = node4
    node2.right= node5
    node3.left = node6
    node3.right = node7
    node4.left = node8
    node4.right = node9
    node5.left = node10


    # 先序遍历
    bt.pre_travel(node1)

3. Advanced features

1. Class attributes and instance attributes

Class attributes are the attributes owned by class objects, which are shared by all instance objects of class objects, and only exist in memorya copy.
In the previous example we encountered theinstance properties (object properties), it is not shared by instance objects of all class objects, and the number of copies in memory depends on the number of objects.

2. Class method and static method

class methodIt is a method owned by a class object, and it needs to be marked as a class method with ==@classmethod==.
1). For a class method, the first parameter must be a class object as the first parameter
(cls is a formal parameter, which can be modified to other variable names, but it is best to use 'cls')
2). It can be accessed through instance objects and class objects.

static methodIt is necessary to use a decorator to identify it as a static method with ==@staticmethod==,
1). Static methods do not need to define more parameters
2). It can be accessed through instance objects and class objects.

insert image description here

insert image description here

3.property class attribute

A special attribute that acts like an instance attribute used and can correspond to a method of a class.
The definition and call of the property attribute should pay attention to the following points:
1. When defining, add the @property decorator on the basis of the instance method; and there is only one self parameter
2. When calling, there is no need for parentheses
insert image description here

"""
类属性应用需求: 对于京东商城中显示电脑主机的列表页面,每次请求不可能把数据库中的所有内容都显示到页面上,而是通过分页的功能局部显示,所以在向数据库中请求数据时就要显示的指定获取从第m条到第n条的所有数据 这个分页的功能包括:
- 根据用户请求的当前页和总数据条数计算出 m 和 n
- 根据m 和 n 去数据库中请求数据

from datetime import  datetime
"""

class Page(object):
    """
    [user1, user2, user3......user100]
    page=2, per_page=10
    第一页: start=0 end=10
    第二页: start=10 end=20
    第三页: start=20 end=30
    ....
    第page页: start=(page-1)*per_page end=page*per_page
    """
    def __init__(self, page, per_page=10):
        self.page = page
        self.per_page = per_page

    # 类属性: 将类方法变成类属性的过程。
    @property
    def start(self):
        return (self.page-1) * self.per_page

    @property
    def end(self):
        return  self.page * self.per_page

if __name__ == '__main__':
    goods = ['good'+str(i+1) for i in range(100)]
    page = Page(page=10, per_page=3)
    print(goods[page.start:page.end])

Simple case of property:

insert image description here

4. Singleton mode

insert image description here

(1) Realize singleton mode based on decorator

insert image description here

(2) Realize the singleton mode based on the new method

insert image description here

Four. Summary

Object Oriented:

    1. 三大特性
        封装(必须要掌握的):
            __new__: 在实例化对象之前执行的, 返回对象。
            __init__: 构造方法, 实例化对象时自动执行。 常用于将对象和属性绑定在一起。
            self: 实质上是对象。
        继承(最多知识点的):
            多继承算法: python3中广度优先算法。
            私有属性和私有方法
        多态(最简单的):

    2. 三大特性的应用
        1). 链表的封装(Leetcode第二题)
        2). 栈的封装
        3). 队列的封装
        4). 二叉树的封装与先序遍历

    3. 高级特性
        1). @classmethod和@staticmethod(类方法和静态方法)
        2). @property类属性
        3). 单例模式: 一个类只能实例化一个对象
            基于装饰器
            基于new方法

Guess you like

Origin blog.csdn.net/Gong_yz/article/details/131117650