Python Basics-10 Introduction to Object-Oriented Programming

Introduction to Object Orientation

  • Process-oriented: write code from top to bottom according to business logic
    • Process-oriented programming focuses on how
      • Realize the requirements step by step from beginning to end
      • According to the development requirements, the function code is encapsulated into one function after another
      • The completed code is to call different functions in sequence
    • Features:
      • Focus on steps and processes, not on division of responsibilities
      • Complicated requirements lead to complex code
      • It is difficult to develop complex projects

  • Object-oriented (OOP): Bind variables and functions together, and encapsulate them by category. Each program is only responsible for the category assigned to itself, enabling rapid development and reducing duplication of code
    • Object-oriented programming, the focus is on who does it
      • First determine the responsibilities
      • Determine different objects according to their responsibilities, and encapsulate different methods inside the objects
      • The code is to call the corresponding methods of different objects in sequence
    • Features:
      • Focus on objects and responsibilities, different objects assume different responsibilities
      • It is more suitable for responding to complex demand changes, and it is specially designed to deal with the development of complex projects, providing fixed routines
      • Need to learn some object-oriented syntax on the basis of process-oriented

classes and objects

  • A class is a general term for a group of things with the same characteristics or behaviors, and is abstract.
    • A feature is actually a variable, which we call an attribute in a class
    • A behavior is actually a function, which we call a method in a class
    • A class is actually an abstract concept composed of attributes and methods .
  • An object is a concrete existence created by a class, which can be used directly
    • The object created by which class has the properties and methods defined in which class

Object Oriented Basic Syntax

  • Define class: how to define class name
    • Define the class name according to the current needs
    • Use class to define a class
    • class Class name: Class names generally need to follow the big hump nomenclature, and the first letter of each word needs to be capitalized
      1. class <class name>:
      1. class <类名>(object):
# 定义类
# 需求:
    # 小明今年 18 岁,身高 1.75 ,每天早上跑完步,会去 吃 东西
    # 小美今年 17 岁,身高 1.65 ,小美不跑步,小美喜欢 吃东西

class Students(object):  # 关注这个类里面有哪些属性和行为
    # 在 __init__方法中,以参数的形式定义特征,我们称之为属性
    def __init__(self,name,age,height):
        self.name = name
        self.age = age
        self.height = height

    # 行为定义为一个个函数
    def run(self):
        print('正在跑步')

    def eat(self):
        print('正在吃东西')

# 调用Students类 Students() ==> 会自动调用 __init__ 方法

# 使用 Students 类创建了两个实例对象 s1 s2
# s1和s2都会有name,age,height属性,同时都有run和eat方法
s1 = Students('小明',18,1.75)
s2 = Students('小美',17,1.65)

# 再根据业务逻辑,让不同的对象执行不通的行为
s1.run()
s1.eat()

s2.eat()

Guess you like

Origin blog.csdn.net/Lz__Heng/article/details/130192244