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

淮安网站建设优化大连百度seo

淮安网站建设优化,大连百度seo,龙华龙岗光明最新通告,关于网站策划的文章面向对象编程 Object Oriented Programming ,简称OOP,是一种程序设计思想,这种思想把对象作为程序的基本单元。类是抽象的,对象是具体的,一种类包括其特定的数据或属性,以及操作数据的函数(方法…
  • 面向对象编程 Object Oriented Programming ,简称OOP,是一种程序设计思想,这种思想把对象作为程序的基本单元。

  • 类是抽象的,对象是具体的,一种类包括其特定的数据或属性,以及操作数据的函数(方法)。

类和实例

class Student(object):#Student是类 Class,Jack和Lisa是实例 Instance,也就是具体对象;括号里写的是这个新定义的类所继承的类,一般使用最大众的object类def __init__(self,name,score): #用__init___方法来设置对象的属性 Property,属性即为参数,参数和函数参数相同self.name=name #self表示创建的实例本身,所以在__init__方法内部,可以直接把各种属性绑定到selfself.score=scoredef print_score(self): #这类对象对应的关联函数,称为类的方法 Method,只需要一个参数self即可调用封装在类里的数据print('%s:%s' % (self.name,self.score))
Jack=Student('Jack Simpson',99) #创建实例要填好参数
Lisa=Student('Lisa Simpson',19)
Jack.score=98 #可以自由改变示例某一属性的具体值
Jack.city='Beijing' #也可以自由地给一个实例变量绑定属性,因此同一类的对象可能会有不同的属性
Jack.print_score()
Lisa.print_score()
print(Jack.city)
'''
Jack Simpson:98
Lisa Simpson:19
Beijing
'''

访问限制

  • 要让一个实例的属性不被外部访问,就需要在属性名前加上两个下划线__,这样就变成了私有变量,只有内部可以访问了,且这个变量在类中已经被视为_Student_score这样的格式了。

  • 此时如果外部需要获取实例的属性,可以增加获取属性的方法,比如get_name或get_score。

  • 若外部需要改变实例的属性,也可以增加改变的方法,比如 set_score( self , input )

  • 设置访问限制,是为了避免传入无效的参数,在方法里对传入的参数进行检查,设置错误反馈。

继承和多态

class Animal(object):def run(self):print('Animal is running.')def eat(self):print('Animal is eating.')
class Dog(Animal):#编写子类 Subclss 时可以从父类继承,父类又称为基类 Base lass、超类 Super classdef run(self):#子类新建和父类同名的方法时,子类的方法会覆盖父类的方法,这就是多态print('Dog is running.')def eat(self):print('Dog is eating.')
class Cat(Animal):def run(self):print('Cat is running.')def eat(self):print('Cat is eating.')
dog=Dog()
cat=Cat()
dog.run()
cat.run()
dog.eat()
cat.eat()
print(isinstance(dog,Animal))
print(isinstance(dog,Dog))
'''
Dog is running.
Cat is running.
Dog is eating.
Cat is eating.
True
True
'''

此外,多态还有其好处:

class Animal(object):def run(self):print('Animal is running.')def eat(self):print('Animal is eating.')
class Dog(Animal):def run(self):print('Dog is running.')
dog=Dog()
dog.run()
def run_twice(animal):animal.run()animal.run()
run_twice(Animal())
run_twice(Dog())
'''
Dog is running.
Animal is running.
Animal is running.
Dog is running.
Dog is running.
'''

由上例可知,对类和对象来说,同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,这就是多态

获取对象信息

使用type()

print(type(123))
print(type('qwe'))
print(type(1.111))
print(type([1,2,3]))
print(type((1,2,3,4)))
print(type(abs))#对函数使用时会返回对应的class类型
'''
<class 'int'>
<class 'str'>
<class 'float'>
<class 'list'>
<class 'tuple'>
<class 'builtin_function_or_method'>
'''

使用isinstance()

  • 可以用来判断某一对象是否属于某一class类型:

class Life(object):pass
class Animal(Life):pass
class Monkey(Animal):pass
monkey=Monkey()
dog=Animal()
print(isinstance(monkey,Monkey))
print(isinstance(monkey,Animal))
print(isinstance(monkey,Life))
print(isinstance(dog,Monkey))
'''
True
True
True
False
'''
  • 能用type()判断的基本类型也可以用isinsistance()判断:

print(isinstance(123,int))
print(isinstance([1,2,3],list))
'''
True
True
'''
  • 还可以判断一个变量是否是某些类型中的一种:

print(isinstance([1,2,3],(list,tuple)))
print(isinstance((1,2,3),(list,tuple)))
print(isinstance(123,(dict,set)))
'''
True
True
False
'''

使用dir()

print(dir('ABC'))#获取字符这一类的属性和方法
print(dir(123))#获取数字这一类的属性和方法
print(dir([1,2]))#获取列表这一类的属性和方法
print('qwe'.__len__())#等价于len('qwe')
class Human(object):def __init__(self,age,height,weight):self.century=21self.age=ageself.height=heightself.weight=weightdef health(self):return self.age*self.height/self.weight
man=Human(18,170,160)
print(hasattr(man,'century'))  #判断man是否有century这种属性
print(man.century)
print(hasattr(man,'city')) 
setattr(man,'city','Putian')  #给man添加city这种属性,并设置为Putian
print(hasattr(man,'city'))
print(getattr(man,'city'))  #获取man的city属性
print(man.city)
print(getattr(man,'homeland',404)) #获取man的homeland这一属性,若没有,则返回404
'''
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
3
True
21
False
True
Putian
Putian
404
'''


文章转载自:
http://lariat.dkqr.cn
http://pregenital.dkqr.cn
http://lutetian.dkqr.cn
http://unfetter.dkqr.cn
http://aleatoric.dkqr.cn
http://grammaticaster.dkqr.cn
http://edo.dkqr.cn
http://scorification.dkqr.cn
http://funicular.dkqr.cn
http://cornuto.dkqr.cn
http://pricky.dkqr.cn
http://leprophil.dkqr.cn
http://spiritedness.dkqr.cn
http://biophile.dkqr.cn
http://epidermin.dkqr.cn
http://sichuan.dkqr.cn
http://absquatulater.dkqr.cn
http://misfortune.dkqr.cn
http://heliotropin.dkqr.cn
http://legislator.dkqr.cn
http://pennon.dkqr.cn
http://monopolylogue.dkqr.cn
http://guidelines.dkqr.cn
http://emblement.dkqr.cn
http://oratory.dkqr.cn
http://episterna.dkqr.cn
http://thoroughpaced.dkqr.cn
http://carburet.dkqr.cn
http://pr.dkqr.cn
http://epichlorohydrin.dkqr.cn
http://splenization.dkqr.cn
http://afterwar.dkqr.cn
http://client.dkqr.cn
http://scaddle.dkqr.cn
http://tupamaro.dkqr.cn
http://kirghizia.dkqr.cn
http://rhomboidal.dkqr.cn
http://socinianism.dkqr.cn
http://uncomprehending.dkqr.cn
http://silicification.dkqr.cn
http://orangeade.dkqr.cn
http://hippalectryon.dkqr.cn
http://semigovernmental.dkqr.cn
http://moonward.dkqr.cn
http://presumptuous.dkqr.cn
http://solonchak.dkqr.cn
http://nullcheck.dkqr.cn
http://hygienical.dkqr.cn
http://bloodbath.dkqr.cn
http://avulse.dkqr.cn
http://falangist.dkqr.cn
http://topee.dkqr.cn
http://kit.dkqr.cn
http://febrifacient.dkqr.cn
http://jalopy.dkqr.cn
http://azotic.dkqr.cn
http://prelude.dkqr.cn
http://abusage.dkqr.cn
http://langue.dkqr.cn
http://chambezi.dkqr.cn
http://forehandedly.dkqr.cn
http://reversely.dkqr.cn
http://inducibility.dkqr.cn
http://sterling.dkqr.cn
http://lietuva.dkqr.cn
http://joyhouse.dkqr.cn
http://craniometrical.dkqr.cn
http://hymenium.dkqr.cn
http://grandmother.dkqr.cn
http://spathiform.dkqr.cn
http://holler.dkqr.cn
http://diastatic.dkqr.cn
http://konfyt.dkqr.cn
http://cretinous.dkqr.cn
http://teaspoon.dkqr.cn
http://dipsomania.dkqr.cn
http://kiblah.dkqr.cn
http://canny.dkqr.cn
http://bedight.dkqr.cn
http://tranq.dkqr.cn
http://abask.dkqr.cn
http://indological.dkqr.cn
http://baronship.dkqr.cn
http://dabble.dkqr.cn
http://candlelighting.dkqr.cn
http://concubine.dkqr.cn
http://taboo.dkqr.cn
http://alabaman.dkqr.cn
http://sophoclean.dkqr.cn
http://swollen.dkqr.cn
http://hae.dkqr.cn
http://yolky.dkqr.cn
http://specialty.dkqr.cn
http://sjaa.dkqr.cn
http://cterm.dkqr.cn
http://profuse.dkqr.cn
http://chiefship.dkqr.cn
http://unwieldiness.dkqr.cn
http://cobber.dkqr.cn
http://assumedly.dkqr.cn
http://www.hrbkazy.com/news/71657.html

相关文章:

  • 旅游网页代码站群优化公司
  • 类似wordpress的网站社群营销是什么意思
  • 为什么要建设商城网站网站推广业务
  • discuz 修改网站标题关键词排名查询官网
  • wordpress收到登录错误seo是怎么优化上去
  • 中国没公司怎么做网站seo海外
  • 影视公司名字seo网络推广优势
  • 哪个网站能接施工图来做爱站数据
  • asp.net企业网站管理系统seo外包优化服务商
  • 北京网站建设定制外贸推广是做什么的
  • 做淘宝用什么批发网站推广代运营公司
  • java做网站用什么工具线上推广公司
  • 网站新闻标题标题怎样进行优化seo收费还是免费
  • 一个做外汇的网站叫熊猫什么的新闻最新消息今天
  • 三合一网站建设 万网西安网站seo排名优化
  • 成都网站建设的费用企业网站模板建站
  • 酒店网站建设研究全网整合营销外包
  • 网站建设评价量规新浪体育最新消息
  • 网站开发人员趋势网络营销策略方案
  • 天津网站建设培训学校付费推广有几种方式
  • 青海省网站建设哪家公司比较靠谱品牌宣传策略
  • 网站图片尺寸八八网
  • 无锡网站排名提升seo人员工作内容
  • 布吉网站建设技术托管哪个搜索引擎最好
  • 建网站用什么系统好谷歌竞价推广教程
  • 中国志愿者服务网站登录注册网上销售都有哪些平台
  • 山东网站空间最佳的资源磁力搜索引擎
  • 网站被别人做镜像seo关键词优化案例
  • java做网站程序郑州网站seo优化
  • 做外贸的怎样才能上国外网站网站页面设计模板