Getting started with python (6): understanding and practice of object-oriented programming

Three programming methods

The programming method that beginners generally accept is " process-oriented programming ", because this method is to write code from top to bottom based on business logic, which is easy to understand and accept.

With the passage of time, " functional programming " appeared , encapsulating certain functions into functions and calling them directly when needed, without repeated writing, saving a lot of time. (Functional programming can be seen on my blog: Getting started with python (two): functions

Next, " object-oriented programming " appeared again , which classified and encapsulated functions into objects, which greatly improved the efficiency of development projects.

Examples:

class Person: #创建类
    def __init__(self,name,age): #__init__()方法称为类的构造方法,注意左右是两个英文下划线
        self.name = name
        self.age = age

    def detail(self): #通过self调用被封装的内容
        print(self.name)
        print(self.age)

object1 = Person('caroline',19)
object1.detail() #python将object1传给self参数,即object1.detail(object1),此时函数内部的self=object1

Insert picture description here
Implement the above example with functional programming:

def detail1(name,age):
    print(name)
    print(age)

object1 = detail1('caroline',19)

Insert picture description here

Two characteristics of object-oriented programming

Object-oriented programming has two major characteristics: encapsulation and inheritance

1. Package

(1) Package content

#封装内容
class Person: #创建类
    def __init__(self,name,age): #__init__()方法称为类的构造方法,注意左右是两个英文下划线
        self.name = name
        self.age = age

object1 = Person('caroline',19) # 将'caroline'和19分别封装到object1及self的name和age属性

Here self is just a formal parameter. When object1 = Person('caroline',19) is executed, self is equal to object1.

(2) Call the encapsulated content

There are two ways to call the encapsulated content: direct call through the object and indirect call through self.

Call directly through the object

#通过对象直接调用object1对象的name和age属性
class Person: #创建类
    def __init__(self,name,age): #__init__()方法称为类的构造方法,注意左右是两个英文下划线
        self.name = name
        self.age = age

object1 = Person('caroline',19) # 将'caroline'和19分别封装到object1及self的name和age属性

print(object1.name)
print(object1.age)

Insert picture description here
Indirect call through self

#通过self间接调用
class Person: #创建类
    def __init__(self,name,age): #__init__()方法称为类的构造方法,注意左右是两个英文下划线
        self.name = name
        self.age = age

    def detail(self): #通过self调用被封装的内容
        print(self.name)
        print(self.age)

object1 = Person('caroline',19)
object1.detail() #python将object1传给self参数,即object1.detail(object1),此时函数内部的self=object1

Insert picture description here
When indirectly called by self, python will pass object1 to the self parameter by default, namely object1.detail(object1), at this time self = object1 inside the method.

The object-oriented programming method does not think in accordance with the execution flow as usual when writing a program. In the above example, a Person class is defined, and various behaviors and characteristics of Person are defined through various functions. It has two names and age. Properties, in the process of calling, let yourself print out.

2. Inheritance

Inheritance is based on ordinary classes to create specialized class objects.

Example explanation:
Cats can: meow, eat, drink, pull, and scatter
Dogs can: barks, eat, drink, pull, scatter

Now if you want to create a class for cats and dogs, you need to implement all their functions for cats and dogs. The pseudo code is as follows:
(The pseudo code cannot be run, mainly for everyone to understand the meaning.)

class 猫: 
    def 喵喵叫(self):
        print ('喵喵叫')
    def(self):
        # do something
    def(self):
        # do something 
    def(self):
        # do something 
    def(self):
        # do something

class 狗:
    def 汪汪叫(self):
        print ('汪汪叫')
    def(self):
        # do something
    def(self):
        # do something 
    def(self):
        # do something 
    def(self):
        # do something

It can be seen from the above pseudo-code that eating, drinking, pulling, and spreading are common features of cats and dogs. It is completely unnecessary to repeat the writing in the code. Here comes the idea of inheritance ! ! !
Can be written as:
Animal: Eat and drink Lazar
Cat: Meow and inherit the characteristics of animals
Dog: Barking and inherit the characteristics of animals

class Animal: #定义动物类
    def eat(self):
        print ("%s 吃 " %self.name)
    def drink(self):
        print ("%s 喝 " %self.name) 
    def shit(self):
        print ("%s 拉 " %self.name) 
    def pee(self):
        print ("%s 撒 " %self.name)

class Cat(Animal): #定义猫类,继承动物类
    def __init__(self, name):
        self.name = name 
    def cry(self):
        print ('喵喵叫')

class Dog(Animal): #定义狗类,继承动物类
    def __init__(self, name):
        self.name = name
    def cry(self):
        print ('汪汪叫')

c1 = Cat('小黑猫') #调用猫类
c1.eat()
c1.cry()


d1 = Dog('小白狗') #调用狗类
d1.drink()
d1.cry()

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45154565/article/details/109318954