Python basics-detailed explanation of classes, objects, and examples

Background: In the process of building a framework to encapsulate functions with python+selenium, I found myself obscured and obstructed, so I opened a basic python column to supplement it.
Summary: Python classes and objects.

Don’t say anything, let me introduce someone to you first~~~

1. Create and use classes.
First of all, you ask me what an object is? I said: Everything is an object.
A monkey is an object with static properties: it is born with two ears and one butt. Dynamic attributes: running, climbing trees, driving if you don’t agree with each other~~

class Dog():
    '''一次模拟小狗的简单尝试'''

    # '属性'
    def __init__(self,name,age):
        '''初始化dog的属性'''
        self.name = name
        self.age = age

    # '方法'
    def sit(self):
        '''模拟小狗蹲下'''
        print(self.name.title() + "is now sitting.")

    def roll_over(self):
        '''模拟小狗打滚'''
        print(self.name.title()+'rolled over!')

# (1)类化为对象,访问属性
a = Dog('habagou1',6).name
b = Dog('habagou2',6).age
print(a,b)
# (2)类化为对象,调用方法
a1 = Dog('habagou3',6).sit()
b1 = Dog('habagou3',6).roll_over()
print(a1)

In actual projects, how do we use classes: code

# 将鼠标的基础操作视为一个类
class Base():

    def __init__(self, driver, log):
        self.driver = driver
        self.log = FrameLog().log()

    # 查找界面上的元素
    def findele(self, *args):
            return self.driver.find_element(*args)

    # 点击
    def click(self, *args):
        return self.findele(*args).click()
	
    # 发送信息
    # noinspection SpellCheckingInspection
    def sendkey(self, args, value):
        self.findele(*args).send_keys(value)

2. Assign default values ​​to attributes, modify attribute values, and increment attribute values

class Dog():
    '''一次模拟小狗的简单尝试'''

    # '属性'
    def __init__(self,name,age):
        '''初始化dog的属性'''
        self.name = name
        self.age = age
        self.a = 0

    # '方法'
    def sit(self):
        '''模拟小狗蹲下'''
        print(self.name.title() + "is now sitting.")

    def roll_over(self):
        '''模拟小狗打滚'''
        print(self.name.title()+'rolled over!')

    def man_bu(self):
        '''小狗漫步'''
        print('小狗向前走了' + str(self.a) + '步')

    def update_man_bu(self,b):
        self.a += b

# # (1)类化为对象,访问属性
# a = Dog('habagou1',6).name
# b = Dog('habagou2',6).age
# print(a,b)
# # (2)类化为对象,调用方法
# a1 = Dog('habagou3',6).sit()
# b1 = Dog('habagou3',6).roll_over()
# print(a1)

# 修改类的默认属性值,且属性值递增
a = Dog('habago4u',6)
print(a.man_bu())

a.update_man_bu(5)
a.update_man_bu(5)
a.update_man_bu(5)
print(a.man_bu())

(3) Class inheritance: The second class in the code has 3 points.

class Dog():
    '''一次模拟小狗的简单尝试'''

    # '属性'
    def __init__(self,name,age):
        '''初始化dog的属性'''
        self.name = name
        self.age = age
        self.a = 0

    # '方法'
    def sit(self):
        '''模拟小狗蹲下'''
        print(self.name.title() + "is now sitting.")

    def roll_over(self):
        '''模拟小狗打滚'''
        print(self.name.title()+'rolled over!')

    def man_bu(self):
        '''小狗漫步'''
        print('小狗向前走了' + str(self.a) + '步')

    def update_man_bu(self,b):
        self.a += b

class huangsegougou(Dog):
    '''这是黄色狗狗'''
    
    # (1)这两行代码中的,super函数让黄色狗狗继承了父类dog的所有属性
    def __init__(self,name,age):
        super(huangsegougou, self).__init__(name,age)
        # (2)将下面的wangcai类的实例直接放在这,只要我调用黄色色狗狗这个类,我的旺财就会“吼一声”
        self.wangcai = Wangcai()
        
    # (3)通过覆盖方法名,直接重写了父类的方法。
    def man_bu(self):
        print('黄色狗狗走了' + str(self.a) + '步')
        
class Wangcai:
    
    def __init__(self):
        print('旺财吼了一声,声如雷!')

4. Import classes
Import classes allow us to focus more on the high-level logic of the main program. The project is mainly concerned with the realization of business logic.
(1) The base.py in the project defines many basic classes, all of which are imported: from base import *
(2) One class can inherit multiple classes. , The classes to be inherited are separated by a comma. The parent class is followed by the parent class in the parentheses after the class name. There can be more than one. If the method of the parent class to be called is different, it is to call the method of a specific parent class. If the calling method of the parent class is in both parent classes, follow the first-come-first-served principle.

class Dituguanli(UnitBase,Base):

    # 如何确保是Base类中的self.find_element()跟unittest中的是一类。
    def test_dianjixinwen(self):
        self.findele(By.XPATH,"//*[text()='新闻']")
        print('跑通了')

Guess you like

Origin blog.csdn.net/weixin_45451320/article/details/112795060