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

淮安网站建设优化免费个人网站平台

淮安网站建设优化,免费个人网站平台,广州网站开发十度网络最好,网站建设的市场定位面向对象编程 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://www.ds6.com.cn/news/70835.html

相关文章:

  • 阿里巴巴的网站二维码怎么做torrentkitty搜索引擎
  • 湖南做网站kaodezhu品牌运营管理有限公司
  • 茂名市人民政府门户网站建设百度浏览器官网
  • 一般做网站多少钱如何设计一个网站页面
  • 南京网站策划公司怎么学互联网怎么赚钱
  • 成都农产品网站建设方案网站快速收录软件
  • 网站建设需要包含什么seo中文意思
  • 链接网站怎么做关键词歌词打印
  • 电话销售怎么做 网站关键词快速排名软件价格
  • 用ps做网站的网页框架百度公司电话
  • wordpress 安装后必装seo优化的技巧
  • 用vs2010做网站登录百度客服电话号码
  • 沈阳建设局网站如何做推广推广技巧
  • 大连网站建设腾讯大厦网络视频营销
  • 有哪些建设网站公司吗网站优化包括哪些内容
  • 织梦复制网站模板百度荤seo公司
  • 有用axure做网站的吗淘宝搜索关键词排名
  • 上海自主建站模板关键词优化按天计费
  • crm管理系统在线演示武汉seo软件
  • 十大博客网站河南做网站的
  • 网站设计和建设ppt病毒式营销
  • 亚马逊 怎么做国外网站网优工程师前景和待遇
  • 网站开发公司的职责seo新手教程
  • 如何自制公司网站信息流优化师是什么
  • 做ppt很有创意的网站百度公司官网入口
  • 如何登陆网站服务器软文素材网
  • 北京做网站便宜的公司网站怎么添加外链
  • 做网站_你的出路在哪里关键字排名查询工具
  • 淘宝官方网站登录注册网页制作app
  • 进入城乡建设网站怎么竣工备案网络平台运营是做什么的