Two ideas of programming : Object oriented and process oriented
The core of process oriented programming is process ( Pipeline thinking ), Process is the step to solve the problem , Process oriented design is like elaborately designing a pipeline , Think about when to deal with something .

Advantage is : It greatly reduces the complexity of writing programs , Just follow the steps to be performed , Just stack the code .

The disadvantage is that : A set of pipeline or process is used to solve a problem , The whole body is moved by the code .

Application scenarios : Once you've completed a scene that's rarely changed , Famous examples are Linux Kernel ,git, as well as Apache HTTP Server etc. .

The core of object-oriented programming is object ( Godlike thinking ), Understand what the object is , You have to think of yourself as God , Everything in the world is the object in God's eyes , What doesn't exist can be created

The advantages and disadvantages of object orientation :
Advantage is : Solved the expansibility of the program . Modify an object individually , It will be reflected in the whole system immediately , For example, it is easy to modify the characteristics and skills of a character parameter in the game .

shortcoming : Poor controllability , It is impossible to pipelined process oriented programming to accurately predict the processing flow and results of problems , Once an object-oriented program is started, it is solved by the interaction between objects , Even God can't predict the end result .

Application scenarios : Software with changing requirements , Changes in general requirements are concentrated in the user layer , Internet applications , Enterprise internal software , Games and so on are good places for object-oriented programming

The two complement each other , It's not the opposite , It's all about solving problems .

1. Classes and objects

Class is a group of similar things . Can help us quickly understand and judge the nature of things .

class : A class of things having the same characteristics ( people 、 Dog 、 The tiger , mobile phone )

object / example : A specific thing ( Your classmate Zhang San 、 Wangcai downstairs )

Instantiation : class ——> Object procedure

For example, when you think of a mobile phone, you think of its shape , There's texting in it , The function of making a phone call , You can also surf the Internet . But can you know which website I'm talking about ?
You can't , So mobile phone is a class , It has the same properties and methods ; And what did you do with your cell phone , Whose cell phone , It's an object of the mobile class .
For example, Xiaomi mobile phone

Python Everything is an object , List objects , Digital objects

Using variables to represent features , Using function to express skills , One kind of things with the same characteristics and skills is ‘ class ’, Object is one of these things .

2. Class creation and writing

a) Define a class

grammar :
class Student:
pass

Class composition :

  • Specification of class name : It's made up of one or more words , The first letter of each word should be capitalized , The rest are in lowercase ;
  • Python Everything is an object , Need to open up memory space
#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sclass Student:
    pass
print(id(Student))print(type(Student))print(Student)

 Insert picture description here
notice ID The numerical , We know that the class here has opened up a memory space .

Case study 1: Student template

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sclass Student:
    native_pace=' Shandong ' #  Variables written directly in the class , Become an attribute  , The local attribute of students is Shandong # Example method :  A definition outside a class is called a function , Defined within a class as a method def __init__(self,name,age):
        self.name=name
        self.age=age  #self.name Called entity attributes , A copy operation was performed , Put the local variable name Properties assigned to entities def eat(self):
        print(' I am eating ') #  This is a way , How to eat , Also called dynamic properties # Static methods @staticmethod
    def method():
        print(' I use the statticmethod To embellish , So I call it static method ')@classmethod# This is called class method def cm(cls):
        print(' I'm a class method , Because I used classmethod To embellish ')

Through this case to understand , Static methods , Example method , And class methods and dynamic properties

b) Create objects

Object creation is also called class instantiation

grammar : Instance name = Class name ()
Class name + Parentheses are instantiations , Will automatically trigger _ init _ Function run , You can use it to customize your own features for each instance .

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sclass Student:
    native_pace=' Shandong ' #  Variables written directly in the class , Become an attribute # Example method :  A definition outside a class is called a function , Defined within a class as a method def __init__(self,name,age):
        self.name=name
        self.age=age  #self.name Called entity attributes , A copy operation was performed , Put the local variable name Properties assigned to entities def eat(self):
        print(' I am eating ')# Static methods @staticmethod
    def method():
        print(' I use the statticmethod To embellish , So I call it static method ')@classmethod# This is called class method def cm(cls):
        print(' I'm a class method , Because I used classmethod To embellish ')##  establish Student The object of stu1=Student(' Internet veteran Xin ',40)stu1.eat()print(stu1.name)print(stu1.age)

 Insert picture description here
The process of instantiation is : class ——> Object procedure .

We had only one Student class , There is a stu1 object , This object has its own specific commands , Age , And the way to eat .

Case study 2

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sclass Person:
    role='person' # The attribute of a character is human def __init__(self,name):
        self.name = name # Each character has its own name def walk(self):
        print("person can walking")## Instantiation process p1=Person(' Zhang San ')  # Class name () It's like execution  Person.__init__()# Here you can understand it as  p1={'name':' Zhang San ',’walk':'walk'}print(p1.name) # View properties directly   Object name , Property name print(p1.walk()) #  Calling method ,  Object name . Method name 

Most of the comments are in the code , Look at the code to understand

Add functions to the code just now , For example, people can not only walk but also *** Dog

class Person:
    role='person' # The attribute of a character is human def __init__(self,name,aggressivity,life_value):
        self.name = name # Each character has its own name self.aggressivity = aggressivity #  Every character has a *** force self.life_value = life_value # Each character has a health value def walk(self):
        print("person can walking")def attack(self,dog):
        dog.life_value -= self.aggressivity## Instantiation process p1=Person(' Zhang San ',10,1000)  # Class name () It's like execution  Person.__init__()# Here you can understand it as  p1={'name':' Zhang San ',’walk':'walk'}print(p1.name) # View properties directly   Object name , Property name print(p1.walk()) #  Calling method ,  Object name . Method name print(p1.aggressivity)print(p1.life_value)

c) Class properties , Class method , Static methods

  • Class properties : Variables outside methods in a class are called class properties , This class is shared by all objects
  • Class method : Use @classmethod Modification methods , Use the class name to directly access
  • Static methods Use @staticmethod The method of decoration , Direct access to the class name

Print(Student.native_place) Access class properties
student.cm Calling class methods
student.sm Call static methods

use Python Object oriented in plants vs zombies

class Flora:
    role='flora' #  Define a plant class , Attributes are plants def __init__(self,name,aggressivity,life_value,money):
        self.name=name
        self.aggressivity=aggressivity ##  Each character has its own *** force self.life_value=life_value # Each character has its own health value self.moeny=money
    def attack(self,corpse):#  Any plant can *** Corpse , Zombie's health value will be based on human's *** And decline corpse.life_value -= self.aggressivity
class Corpse:
    rols='corpse'  # Zombies , Attributes are zombies def __init__(self,name,aggressivity,life_value):
        self.name = name
        self.aggressivity = aggressivity  ##  Each character has its own *** force self.life_value = life_value  #  Each character has its own health value def eat(self,flora):
        flora.life_value-=self.aggressivity# Zombies can eat plants , The vitality of plants will decline class Weapon: ##  arsenal def __init__(self,name,price,aggrev,life_value):
       self.name=name
       self.price=price
       self.aggrev=aggrev
       self.life_value=life_value
    def update(self,obj):
        obj.money -= self.price  #  The plants that buy weapons cost less obj.aggressivity += self.aggrev  #*** Force increases obj.life_value += self.life_value  # Health increased def prick(self,obj):
        obj.life_value -= 500 #  The weapon trick , One drop of blood 500ps=Weapon(' Pea cannon ',200,6,100) ## There is a weapon to choose from , The weapon's name is pea cannon cabbage=Flora('bigcabbage',10,1000,6000) # Created the Chinese cabbage plant zombie=Corpse('z1','zob1',10) # Created a zombie , Ordinary zombies ## Plants can't kill zombies , So I upgraded my weapon to pea cannon print(cabbage.moeny,cabbage.life_value,cabbage.aggressivity)print(zombie.life_value)