Software testing/test development丨Python object-oriented programming ideas

What is object-oriented

  • Python is an object-oriented language
  • Object-Oriented Programming( OOP):Object Oriented Programming

The so-called object-oriented means to simulate the real world as much as possible when programming, handle problems according to the logic in the real world, analyze which entities are involved in the problem, and what attributes and methods these entities should have. We How to solve the problem by calling the properties and methods of these entities.

Two programming ideas

  • process oriented

    • A process-centered programming idea
    • First analyze the steps required to solve the problem
    • Then use functions to implement these steps step by step
    • Finally, call and run in sequence.
  • object-oriented

    • It is a programming idea that is more in line with our human thinking habits
    • Object-oriented development means constantly creating objects, using objects, and operating objects to do things.
    • Can simplify complex things

Classes and Objects

  • Class ( class): Used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection.
  • Object ( object): Also called an instance of a class, it is a concrete entity.

Class definition

  • class Keywords
# 语法
class 类名(父类名):
    """类的帮助信息"""
    属性
    方法
# class_def.py

# 类的声明
class Human(object):
    """人类"""

    # 定义属性(类属性)
    message = "这是类属性"


# 通过类访问类属性
print(Human.message)

class methods

  • instance method

    • Construction method
  • class method

  • static method

Constructor and instantiation

  • Role: Instantiate objects
  • grammar:def __init__(self, 参数列表)
  • access:类名(参数列表)
# constructor_method.py

class Human:

    # 定义属性(类属性)
    message = "这是类属性"

    # 构造方法
    def __init__(self, name, age):
        # 实例变量
        self.name = name
        self.age = age
        print("这是构造方法")


# 实例化对象
person = Human("哈利波特", 12)

# 通过实例访问类属性
print(person.message)

# 通过实例访问实例属性
print(person.name)
print(person.age)

instance method

  • Role: Provide methods shared by instances of each class
  • grammar:def 方法名(self, 参数列表)
  • access:实例.方法名(参数列表)
# instance_method.py


class Human:

    # 实例方法
    def study(self, course):
        print(f"正在学习{course}")

# 实例化
person = Human()

# 通过实例访问实例方法
person.study("python")

class method

  • Function: Detailed information of the class can be manipulated
  • grammar:@classmethod
  • access:类名.类方法名(参数列表)
  • The difference between class methods and ordinary methods is that class methods can only access class variables and cannot access instance variables.
# class_method.py

class Human:

    # 类属性
    population = 0

    # 类方法
    @classmethod
    def born(cls):
        print("这是类方法")
        cls.population += 1


# 通过类名访问类方法
Human.born()
print(Human.population)

static method

  • @staticmethod
  • No class attributes and class methods cannot be called in static methods of a class.
# static_method.py

class Human:

    # 静态方法
    @staticmethod
    def grow_up():
        print("这是静态方法")


# 通过类名访问静态方法
Human.grow_up()

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/YLF123456789000/article/details/135294992