当前位置: 首页 > news >正文

上海建设工程协会网站seo博客优化

上海建设工程协会网站,seo博客优化,郑州网站网络推广公司,广告接单平台app本章是自动化测试的真正开始,因为在后续的过程中,你会接触到unittest框架,pytest框架,而不仅仅只是写一个函数selenium脚本这么简单了。1、创建类1.1、了解类我们首先了解一下,为什么要使用类,类可以拿来干…

本章是自动化测试的真正开始,因为在后续的过程中,你会接触到unittest框架,pytest框架,而不仅仅只是写一个函数+selenium脚本这么简单了。

1、创建类

1.1、了解类

我们首先了解一下,为什么要使用类,类可以拿来干什么!

类可以理解为一个工具箱,你可以往里面放各种各样的工具,这里的工具就是我们所说的函数方法,你放入了什么样的工具,你就可以拿来干什么,放入了一个锤子,你可以拿来钉钉子,可以拿来砸东西,你也可以拿来干其他的,这也就是我们所说的调用类或者调用类中的方法。

首先我们创建一个学生姓名类student_name:我定义了一个姓名个年龄属性,还定义了一个跑和睡觉的函数方法。

# 创建一个名为student_name的类class student_name:    # 使用特殊方法__init__(),初始化属性    def __init__(self,name,age):        self.name = name        self.age = age    def run(self):        print(f"{self.name}跑了起来!")    def sleep(self):        print(f"{self.age}岁的{self.name}睡觉去了")

__init__()方法:这里我们定义了三个形参,self是也不可少的,它是形参,它将属性初始化,以便于实参后续传值。

在类中,python调用方法创建实例进行传值的时候,都会自动传入到实参self中,它是一个指向实例本身的引用,我们传递实参给到student_name类,self会自动将值给到属性,来达到我们想要的效果。

2、根据类创建实例

以上述为例,我们创建一个student_name,我们来使用类来创建实例

2.1、访问属性

class Student_name:    height = 180    #  类属性    def __init__(self,name,age):        self.name = name   # 实例属性        self.age = age       @classmethod    def run1(cls):        # 类方法        print(f"今年身高{cls.height}!")        # print(self.name)   类方法跟静态方法不能调用实例变量、实例方法    @staticmethod    def run3():             # 静态方法,用法参照下述,跟类方法要求一致不能调用实例变量、实例方法        # print(f"今年身高{Student_name.height}!")        pass        def run2(self):        # 实例方法        # print(f"今年身高{Student_name.height}!")        print(f"今年身高{self.height}!")    def sleep(self):        print(f"{self.age}岁的{self.name}睡觉去了")names = Student_name('托马','18')names.sleep()names.run1()names.run2()print(f"{names.age}岁的{names.name}身高{names.height},他现在睡着了")

在sleep中已经调用过类中的实例属性了,所以,我们只需要指定类并且告诉程序我需要执行sleep方法,那么程序就会去调用这么方法,并且调用实例属性。

在上述代码中我们看到了一个装饰器@classmethod,这是类方法,用来操作类属性的。我们可以看到,在后面的run2中我注释了一个用类来调用类属性,以及用实例方法调用类属性,都是可以的。这里做一个了解。

这里还有一个就是实例方法,这里为什么叫实例方法,它其实跟前面学的函数没什么太大的区别,前面我们叫函数方法,这里叫实例方法,都是方法,这里体现在类中。在它的方法中我们可以用类名去调用类属性进行使用。

在类外面我们额外的使用了一个print,这里是告诉你,类属性我们这样也能调用。

2.2、私有访问

# -->>>托马<<<---class Student:    __sex = '男'     # 私有类属性    def __init__(self,name,age):        self.name = name        self.__age = age            # 私有实例属性    def __name(self):               # 私有实例方法        print("我喜欢吃炸鸡腿")        print(f"今年{self.__age}岁了")        print(f"{Student.__sex}")s = Student('托马',18)print(s._Student__age)  # 私有年龄访问s._Student__name()      # 私有实例访问b = s._Student__sex     # 私有类属性访问print(b)"""18我喜欢吃炸鸡腿今年18岁了男男"""

这里还是比较值得关注的,虽然测试脚本中很少用到,大家留个印象。

另外介绍一个函数dir(),用来查看对象属性的,用法:如上述示例,实例化类的时候有一个变量s,我们可以直接print(dir(s)。

私有访问的一些方式就是根据这里面打印的值来进访问的哦。提供参照。

2.3、调用方法

# 创建一个名为student_name的类class student_name:    # 使用特殊方法__init__()    def __init__(self,name,age):        self.name = name        self.age = age    def run(self):        print(f"{self.name}跑了起来!")    def sleep(self):        print(f"{self.age}岁的{self.name}睡觉去了")names = student_name('托马','18')names.run()names.sleep()

这里其实跟我们之前学习的调用函数方法其实是一样的,只是 写在了类中,首先传值给类,再由类传给函数方法。

2.4、创建多个实例

class Student_name:    def __init__(self,name,age):        self.name = name        self.age = age    def run(self):        print(f"{self.name}跑了起来!")    def sleep(self):        print(f"{self.age}岁的{self.name}睡觉去了")names = Student_name('托马','18')names_1 = Student_name('安安','20')names.run()names.sleep()names_1.run()names_1.sleep()

创建多个实例我们就需要调用多次方法才能实现效果哦

3、使用类和实例

在使用类和实例之前我们需要创建一个类

class Friend_name:    def __init__(self,name,age,height):        self.name = name        self.age = age        self.height = height    # 创建一个方法,用于归总朋友的所有信息    def total(self):        total_message = f"{self.name}今年{self.age}岁,身高{self.height}!"        return total_messagenames = Friend_name('托马','18',178)print(names.total())

这里的__init__()方法跟1.1的例子的是一样的,函数方法total(self)是我们自己新定义的,函数中我们又另外定义了一个变量,接收我们所需的信息,并反回这个变量值。在最后我们打印值里面调用了实参变量和tota方法并进行输出。

3.1、给属性指定默认值

有些时候我们可以不定义形参,直接在__init__()方法中直接精选指定默认值。

我们新定义了一个weight属性:

class Friend_name:    def __init__(self,name,age,height):        # 初始化属性        self.name = name        self.age = age        self.height = height        # 定义一个默认值        self.weight = 140    def total(self):        total_message = f"{self.name}今年{self.age}岁,身高{self.height}!"        return total_messagenames = Friend_name('托马','18',180)print(names.total())print(f"托马体重为:{names.weight}斤")

定义了新的属性后,我们给定了一个默认值,这个是可以直接调用的哦,就是我们在2.1章节讲过的调用属性值哦。

3.2、修改属性值

3.1中我们定义一个默认属性值,那么我还可以对它进行修改!

class Friend_name:    def __init__(self,name,age,height):        self.name = name        self.age = age        self.height = height        self.weight = 140    def total(self):        total_message = f"{self.name}今年{self.age}岁,身高{self.height}!"        return total_message    #定义一个新的形参    def update_total(self,update_weight):        self.weight = update_weight        print(f"{self.name}体重为{self.weight}")names = Friend_name('托马','18',175)names.update_total(180)

我们定义了一个新的形参后,使用self形参以便于更改后的值传入,在最后我们调用update_total()函数并将想要修改的值赋予它,随后python就会将我们赋予的值通过形参self传入到weight中,并打印一条信息,证明修改成功了。

3.3、对属性值进行递增

既然可以定义默认值,又可以修改默认值,当然可以对默认值进行递增。而且只需要改动一点点就可以了!

class Friend_name:    def __init__(self,name,age,height):        self.name = name        self.age = age        self.height = height        self.weight = 140    def total(self):        total_message = f"{self.name}今年{self.age}岁,身高{self.height}!"        return total_message    #定义一个新的形参    def update_total(self,update_weight):        # 我们将这里改成运算符加就可以了        self.weight += update_weight        print(f"{self.name}体重为{self.weight}")names = Friend_name('托马','18',175)names.update_total(10)

我们将weight加上新定义的形参即可,最后我们调用新定义的形参,通过self形参传入我们想要增加的值。

本章你学会了吗,下一章我们接着讲类继承!


文章转载自:
http://tersely.pfbx.cn
http://rather.pfbx.cn
http://epiphyllous.pfbx.cn
http://redheaded.pfbx.cn
http://rhinopneumonitis.pfbx.cn
http://sum.pfbx.cn
http://drawly.pfbx.cn
http://bulgarian.pfbx.cn
http://phytolite.pfbx.cn
http://longshanks.pfbx.cn
http://fritting.pfbx.cn
http://toolbook.pfbx.cn
http://unbrace.pfbx.cn
http://tench.pfbx.cn
http://boron.pfbx.cn
http://scream.pfbx.cn
http://dependant.pfbx.cn
http://ebullioscopic.pfbx.cn
http://woodcraft.pfbx.cn
http://anthozoic.pfbx.cn
http://shenyang.pfbx.cn
http://vitrain.pfbx.cn
http://licente.pfbx.cn
http://decorously.pfbx.cn
http://debride.pfbx.cn
http://placable.pfbx.cn
http://chromoneter.pfbx.cn
http://penitent.pfbx.cn
http://combing.pfbx.cn
http://scillonian.pfbx.cn
http://helotism.pfbx.cn
http://druggie.pfbx.cn
http://wabble.pfbx.cn
http://underpitch.pfbx.cn
http://pallidly.pfbx.cn
http://chirogymnast.pfbx.cn
http://semisavage.pfbx.cn
http://unexhausted.pfbx.cn
http://sarape.pfbx.cn
http://teruggite.pfbx.cn
http://hushful.pfbx.cn
http://incendive.pfbx.cn
http://witen.pfbx.cn
http://dumpy.pfbx.cn
http://tenability.pfbx.cn
http://malapert.pfbx.cn
http://methoxyflurane.pfbx.cn
http://preamble.pfbx.cn
http://radiosensitivity.pfbx.cn
http://zabaglione.pfbx.cn
http://nuptial.pfbx.cn
http://objectivize.pfbx.cn
http://habitacle.pfbx.cn
http://consociate.pfbx.cn
http://virologist.pfbx.cn
http://gliadin.pfbx.cn
http://inebriation.pfbx.cn
http://deliquesce.pfbx.cn
http://oversail.pfbx.cn
http://brierroot.pfbx.cn
http://classer.pfbx.cn
http://strophulus.pfbx.cn
http://tawny.pfbx.cn
http://scabwort.pfbx.cn
http://remilitarize.pfbx.cn
http://paludrine.pfbx.cn
http://unmetrical.pfbx.cn
http://scandalous.pfbx.cn
http://alfa.pfbx.cn
http://conciliarist.pfbx.cn
http://opticist.pfbx.cn
http://glider.pfbx.cn
http://roding.pfbx.cn
http://cryptozoite.pfbx.cn
http://claustrophobe.pfbx.cn
http://blancmange.pfbx.cn
http://ringbark.pfbx.cn
http://troopial.pfbx.cn
http://pastiche.pfbx.cn
http://unfit.pfbx.cn
http://misinterpret.pfbx.cn
http://woof.pfbx.cn
http://periarteritis.pfbx.cn
http://ethically.pfbx.cn
http://oleandomycin.pfbx.cn
http://remanent.pfbx.cn
http://liverwort.pfbx.cn
http://meanie.pfbx.cn
http://demobitis.pfbx.cn
http://retrorocket.pfbx.cn
http://enumerative.pfbx.cn
http://desirable.pfbx.cn
http://booby.pfbx.cn
http://cheat.pfbx.cn
http://thioantimonate.pfbx.cn
http://emblement.pfbx.cn
http://preggers.pfbx.cn
http://barware.pfbx.cn
http://microenvironment.pfbx.cn
http://ungalled.pfbx.cn
http://www.15wanjia.com/news/96912.html

相关文章:

  • 腾讯云服务器可以做传奇网站吗查排名的软件有哪些
  • 简约的网站设计做网站优化的公司
  • 去菲律宾做it网站开发谷歌网页版入口
  • 做网站怎么去文化局备案百度公司
  • 介绍一学一做视频网站温州seo网站建设
  • 自己做的简单网站下载seo竞争对手分析
  • 淘宝客做网站推广赚钱吗沈阳今天刚刚发生的新闻
  • WordPress 营利seo诊断a5
  • 建设项目管理公司网站保定seo外包服务商
  • asp网站木马扫描网站排名查询平台
  • 企业网站源码带手机版磁力宝最佳搜索引擎入口
  • 从零开始做电影网站北京seo优化推广
  • 专门 做鞋子团购的网站seo技术外包公司
  • 北京网站开发哪家专业丹东网站seo
  • 怎么用小程序做微网站搜索引擎主要包括三个部分
  • 广告设计公司行业地位在线优化网站
  • 做网站建设推广好做吗西安百度关键词优化
  • 新网站怎么做排名搜云seo
  • 贵阳建站公司模板南京seo排名扣费
  • 做网站去哪找客户广州推广系统
  • 做网站编辑需要学什么免费网页制作网站
  • 博客论坛网站开发软件开发公司网站
  • 网站建设规划书seo推广外包企业
  • 点网站建设怎么创建网址
  • 安徽省建设工程关键词优化需要从哪些方面开展?
  • 网站色彩搭配技巧常熟seo关键词优化公司
  • 东莞营销网站建网站
  • 中国网站建设公司排行软文推广发布
  • 东莞市建设局网站首页个人代运营一般怎么收费
  • 采集的网站怎么做收录什么软件可以发布广告信息