




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一、 类和对象的概念对象=属性+行为(方法)把具有相同属性和方法的对象归为一类。类是对象的抽象化,对象是类的实例化。二、 类的定义、实例化、属性和方法的调用class 类名: 属性方法 class people:name=jimage=20def printName(self):print jim people.pTraceback (most recent call last): File , line 1, in people.pAttributeError: class people has no attribute p people.printName()Traceback (most recent call last): File , line 1, in people.printName()TypeError: unbound method printName() must be called with people instance as first argument (got nothing instead) people.printName(self)Traceback (most recent call last): File , line 1, in people.printName(self)NameError: name self is not defined p=people() jim p.age20 p.printName()jim class people1:_name=mike_age=20sex=wemom p1=people1() p1._nameTraceback (most recent call last): File , line 1, in p1._nameAttributeError: people1 instance has no attribute _name p1._ageTraceback (most recent call last): File , line 1, in p1._ageAttributeError: people1 instance has no attribute _age p1.sexwemom p1.sexwemom类的内置函数class c1:def tell(self):print this is a classdef _init_(self):print c1 is initdef _del_(self):print c1 is del c11=c1()c1 is init c11=0c1 is del三、 类属性、实例属性、类方法、实例方法、静态方法 class people:name=alice_age=13 p=people() alice p._ageTraceback (most recent call last): File , line 1, in p._ageAttributeError: people instance has no attribute _age实例对象可以调用类里面定义的公有属性,不能调用私有属性。 alice people._ageTraceback (most recent call last): File , line 1, in people._ageAttributeError: class people has no attribute _age类对象可以调用类里面定义的公有属性,不能调用私有属性。 p.sex=man #sex是实例属性 people.sexTraceback (most recent call last): File , line 1, in people.sexAttributeError: class people has no attribute sex 实例属性不能被类对象调用 p2=people() p2.sexTraceback (most recent call last): File , line 1, in p2.sexAttributeError: people instance has no attribute sex实例属性不能被其他实例对象调用 p.sexman实例属性只能被定义该属性的实例对象调用 class people:country=chinaclassmethod #声明为类方法def getCountry(cls):return cls.country people.getCountry()china类方法可以被类对象调用 p=people() p.getCountry()china类方法可以被实例对象调用 class people:country=chinaclassmethoddef getCountry(cls):return cls.countryclassmethoddef setCountry(cls,a): #使用类方法修改类属性cls.country=a return a p=people() p.getCountry()china p=people() p=people() p.setCountry(a)a people.setCountry(b)b class people:country=chinadef getCountry(self): #实例属性return self.country people.getCountry()Traceback (most recent call last): File , line 1, in people.getCountry()TypeError: unbound method getCountry() must be called with people instance as first argument (got nothing instead)实例方法不能被类对象访问 p=people() p.getCountry()china实例方法可以被实例对象访问 class people:country=chinastaticmethod #声明为静态方法,静态方法不需要参数def getCountry():return people.country print people.getCountry()China静态方法可以被类对象访问p=people() p.getCountry()china静态方法也可以被实例对象访问四、 继承单继承:class student: name=aa age=13 def _init_(self): =bb self.grade=high self._sex=man print initing def _del(self): print dead def eat(self): print i am eating staticmethod def runSelf(): print in static method def _hello(self): print helloclass miniStudent(student): passmini=miniStudent()print mini._hello()多继承:class studentA: def _init_(self): print init studentA def smile(self): print smileclass student: name=aa age=12 def _init_(self): self.addr=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 近期人缘测试题及答案
- 2025年北京市建筑工程钢筋采购合同(GC)
- 信息处理技术员考前必看试题及答案
- 2025展览会合作合同模板
- 2025长春市租赁合同
- 解析法学概论中的行政法要素试题及答案
- 美容护肤仪器市场分析考核试卷
- 电力设备温度监测系统考核试卷
- 网络管理员考试理论与实践试题及答案
- 2025饮品投资合同模板
- 《大客户销售培训》课件
- 多学科协作危重孕产妇管理制度
- GB 4793-2024测量、控制和实验室用电气设备安全技术规范
- 24秋国家开放大学《教育心理学》终结性考核论文大作业参考答案
- DB35T 2032-2021 耕地质量监测与评价技术规程
- 《证券投资学》全套教学课件
- 2024年秋新北师大版七年级上册数学教学课件 第五章 一元一次方程 第4节 问题解决策略:直观分析
- DLT5196-2016 火力发电厂石灰石-石膏湿法烟气脱硫系统设计规程
- DL∕ T 802.3-2007 电力电缆用导管技术条件 第3部分:氯化聚氯乙烯及硬聚氯乙烯塑料电缆导管
- CJT 511-2017 铸铁检查井盖
- 活动执行实施合同范本
评论
0/150
提交评论