python类和对象.doc_第1页
python类和对象.doc_第2页
python类和对象.doc_第3页
python类和对象.doc_第4页
python类和对象.doc_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论