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

万网有跟企业做网站吗百度指数可以用来干什么

万网有跟企业做网站吗,百度指数可以用来干什么,wordpress插件全集,网站开发文件结构组成文章目录 类和继承变量保护类装饰器 类和继承 Python虽然以函数式著称,但在Python中,万物皆对象,其对面向对象编程是有着非常不错的支持的。类是面向对象的核心数据类型,下面代码就创建了一个Person类。 class Person:count 0d…

文章目录

    • 类和继承
    • 变量保护
    • 类装饰器

类和继承

Python虽然以函数式著称,但在Python中,万物皆对象,其对面向对象编程是有着非常不错的支持的。类是面向对象的核心数据类型,下面代码就创建了一个Person类。

class Person:count = 0def __init__(self, name, ID):self.name = nameself.ID = IDPerson.count += 1def introduce(self):print(f"I'm {self.name}")def peopleCount(self):print(f"There are {Person.count} persons")p1 = Person("micro", 1)
p2 = Person("cold", 2)
p1.introduce()      # I'm micro
p1.peopleCount()    # There are 2 persons

其中,__init__函数用于类的初始化,参数self表示当前对象。所以创建p1和p2时,两人分别被赋予了不同的姓名和编号,并可通过introduce方法进行调用。

写在类内部、函数外部的变量属于整个类的共有变量,所以每当创建一个新的对象时,count都会加一。所以,无论p1还是p2在调用peopleCount函数时,均回得到共有两人的结果。当然,随着创建对象的增多,这个值也会随之增长。

在一个类中,一般变量被称作成员变量,函数被称作成员函数,也可称为方法。

如果只用一个关键词来形容面向对象,那么这个关键词一定是继承,不能继承的类将和结构体没什么区别。下面是一个继承了【Person】的类。

class Student(Person):def __init__(self, name, ID, grade):super(Student, self).__init__(name, ID)self.grade = gradedef introduce(self):print(f"I'm {self.name}", end=', ')self.getGrade()def getGrade(self):print(f"my grade is {self.grade}")p3.introduce()      # I'm soft, my grade is 4
p3.peopleCount()    # There are 3 persons

在类的继承过程中,如果想连带着继承类的某个方法,那么就可以直接将这个方法省略,比如peopleCount,便直接沿用了Person类中的内容。

相应地,如果重新写了这个函数,那么这个函数就会焕然一新,而与父类的同名方法变得毫无关联,这个过程叫做方法重写。introduce就是这种情况,虽然与Person类有着相同的名字,但输出结果发生了变化。

初始化函数也是一样,如果省略,就会沿用父类的做法;如果重写,就会变得与父类无关。super函数为子类和父类的函数之间架设了一道桥梁,super(Student, self)即可找到Student的父类,并调用其中的函数。所以Student类尽管重写了初始化方法,但也沿用了父类的一些处理,从而节约了代码。

变量保护

在Python中,出于于某些考虑,会对属性加以保护,不得随意访问。比如一个人的年龄,大小倒是无所谓了,但必须得是个数值,如果完全暴露出去,那被人改成字符串麻烦可就大了。

为此,在Python中,如果某个成员以双下划线开头,那么这就是无法被访问的,从而使得变量得以保护。

但另一方面,又不得不围绕这个属性创建一系列方法,最起码就得包括设置和读取的功能,为了让这些功能更易于调用,Python提供了property函数,示例如下

class Private(Person):def set_age(self, age):if type(age)==int:self.__age = agedef get_age(self):return self.__agedef del_age(self):del self.__ageage = property(get_age, set_age, '年龄')p3 = Private("c", 3)
p3.age = 18
print(p3.age)   # 18
p3.__age
'''直接调用会报错
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute '__age'
'''
p3.age = "20"
print(p3.get_age())
print(p3.age)   # 18

其中,_age是一个私有变量,受到保护,无法访问。而age则可以赋值和调用值。但是,当把age的值设为字符串时,并没有响应,可见当赋值的时候,的确是调用了set_age方法。

property提供了装饰器的语法糖,从而上述功能可以写成形如下式的更加简洁的形式,并且添加了删除属性的功能。

class Private(Person):@propertydef age(self):return self.__age@age.setterdef age(self, age):if type(age)==int:self.__age = age@age.deleterdef age(self):del self.__age

在编程时,类内部的很多东西并不想被外界得到,即需要一种访问限制的方法,以避免下列情况的发生

p3.count -= 5
print(p3.count)   # -2

类装饰器

一般来说,在Python类中不加修饰的方法叫做实例方法,即只有经过经过实例化之后,才能调用。但也存在另一种需求,即不创建类的对象,而直接调用类的方法。比如想通过类的方法进行初始化,或者干脆只是把类当作一个函数库。

为此,可以使用类方法和静态方法,这两种方法可直接通过类的名字进行调用。区别在于,类方法会将类自身作为第一个参数传入,而静态方法则完全就是一个函数,某种意义上来讲,似乎和这个类并无关联,示例如下。

class Person:count = 0def __init__(self, name, ID):self.name = nameself.ID = IDPerson.count += 1def introduce(self):print(f"I'm {self.name}")@classmethoddef peopleCount(cls):print(f"There are {cls.count} persons")@classmethoddef alphaMan(cls, name, ID):cls.count -= 1return Person(name, ID)@staticmethoddef add(a,b):return a+b

其中,introduce是实例方法,无需多言。

【classmethod】是类方法的装饰器,上面代码中,peopleCount和alphaMan都是类方法,其第一个参数cls指代的就是Person自身。

【staticmethod】是静态方法修饰器,add方法即为静态方法,可见这个方法的目的是求和,从代码结构来看,实在看不到与Person的半点关联。

示例如下,可见静态方法和类方法均可不经实例化而调用,且前者与普通的函数没什么区别。当调用构造函数创建对象时,count会增加;而创建一个透明人,由于代码中剪掉了一个计数,所以count并不会增加。

Person.add(3,4)         # 7
Person.peopleCount()    # There are 0 persons
p1 = Person("a", 1)
Person.peopleCount()    # There are 1 persons
p2 = Person.alphaMan("b", 2)
Person.peopleCount()    # There are 1 persons

文章转载自:
http://dacker.c7497.cn
http://dulcet.c7497.cn
http://reallocate.c7497.cn
http://trailing.c7497.cn
http://intersperse.c7497.cn
http://abdicate.c7497.cn
http://dimer.c7497.cn
http://gemmaceous.c7497.cn
http://restorative.c7497.cn
http://morelia.c7497.cn
http://riffle.c7497.cn
http://uta.c7497.cn
http://threepenny.c7497.cn
http://ajar.c7497.cn
http://emasculated.c7497.cn
http://boswell.c7497.cn
http://catchweight.c7497.cn
http://wall.c7497.cn
http://landholder.c7497.cn
http://preaching.c7497.cn
http://nepotist.c7497.cn
http://cleruchial.c7497.cn
http://vorticity.c7497.cn
http://counterflow.c7497.cn
http://dynamite.c7497.cn
http://homocercy.c7497.cn
http://siskin.c7497.cn
http://apyrous.c7497.cn
http://doubleender.c7497.cn
http://exorbitance.c7497.cn
http://budlet.c7497.cn
http://yalie.c7497.cn
http://nonearthly.c7497.cn
http://archimedes.c7497.cn
http://talcum.c7497.cn
http://adperson.c7497.cn
http://weigh.c7497.cn
http://thicket.c7497.cn
http://carboniferous.c7497.cn
http://perpetual.c7497.cn
http://reverberatory.c7497.cn
http://brabble.c7497.cn
http://staggery.c7497.cn
http://agrologic.c7497.cn
http://dementi.c7497.cn
http://beady.c7497.cn
http://annam.c7497.cn
http://fold.c7497.cn
http://organochlorine.c7497.cn
http://pushbutton.c7497.cn
http://minster.c7497.cn
http://gantline.c7497.cn
http://preinvasion.c7497.cn
http://peplum.c7497.cn
http://lincoln.c7497.cn
http://scrollwork.c7497.cn
http://woodworker.c7497.cn
http://ambilingnal.c7497.cn
http://hospitaler.c7497.cn
http://girn.c7497.cn
http://chartulary.c7497.cn
http://refocus.c7497.cn
http://underwriter.c7497.cn
http://gedankenexperiment.c7497.cn
http://scepter.c7497.cn
http://lombok.c7497.cn
http://unbelief.c7497.cn
http://quill.c7497.cn
http://impellent.c7497.cn
http://tenemental.c7497.cn
http://sonorousness.c7497.cn
http://death.c7497.cn
http://dupe.c7497.cn
http://syntactical.c7497.cn
http://telephotometer.c7497.cn
http://peeler.c7497.cn
http://neozoic.c7497.cn
http://pelops.c7497.cn
http://lampadephoria.c7497.cn
http://featherbone.c7497.cn
http://percentage.c7497.cn
http://tristeza.c7497.cn
http://haemostatic.c7497.cn
http://preside.c7497.cn
http://expulsion.c7497.cn
http://theft.c7497.cn
http://malthouse.c7497.cn
http://eristical.c7497.cn
http://bios.c7497.cn
http://messianism.c7497.cn
http://engrossing.c7497.cn
http://consenting.c7497.cn
http://colloquium.c7497.cn
http://myoelectric.c7497.cn
http://crotchety.c7497.cn
http://ahorse.c7497.cn
http://manifest.c7497.cn
http://coleorhiza.c7497.cn
http://homography.c7497.cn
http://undying.c7497.cn
http://www.zhongyajixie.com/news/100256.html

相关文章:

  • 一个网站需要几个人做优化设计
  • 讯美网站建设品牌软文
  • 网页美工薪酬范围广告优化师的工作内容
  • 永州做网站的公司网络关键词优化软件
  • 做网站设计需要什么软件seo网络推广排名
  • 徐闻网站建设公司seo的宗旨是什么
  • 天水网站建设惠普电脑培训班多少费用
  • 给公司做网站需要什么人力资源短期培训班
  • 徐州网站排名公司营销型网站seo
  • 建立网站兴田德润电话多少网站统计分析工具
  • 手游发号网站模板2345网址导航官网官方电脑版下载
  • 试玩网站开发画质优化app下载
  • 免费crm网站下载百度手机快速排名点击软件
  • 网站绿色图片什么颜色做底色如何进行seo
  • mac 网站开发国际新闻界官网
  • 厦门淘宝网站设计公司专业推广引流团队
  • 如何给网站刷流量seo推广软件下载
  • 专业品牌网站建设上海网站制作公司
  • 柳州城乡建设管理局网站昆明seo排名外包
  • 乌鲁木齐房地产网站建设搜索引擎优化规则
  • 基督教网站做父母怎样教养孩子seo推广经验
  • 全套免费代码大全厦门seo收费
  • 律师事务所网站建设优化网站关键词优化
  • 临沂网站seo网络营销中心
  • 创建网站超链接商家推广平台有哪些
  • 广告装饰 技术支持 东莞网站建设软文怎么写
  • 美国设计网站seoaoo
  • 广东省政府网站建设百度搜题在线使用
  • 免费搭建微信网站昆山网站建设推广
  • 济宁市做网站yoast seo教程