Python学习系列(八)( 面向对象基础)
1,域:属于一个对象或类的变量。有两种类型,即实例变量—属于每个实例/类的对象;类变量—属于类本身。2,类的方法:对象也可以使用属于类的函数来具有功能,这样的函数称之为类的方法。域和方法合称为类的属性。类使用class关键字创建,类的属性被列在一个缩进块中。3,self:类的方法与普通的函数只有一个特别的区别----他们必须有一个额外的第一个参数名称,但是在调用的时候不能为其赋值,Python会提供。这个特别的变量指对象本身,按照惯例命名为self。self等价于C++中的self指针和Java,C#中的this。创建一个类:class Person:
pass p=Person()
print p
>>> ============= RESTART ================================
>>>
<__main__.Person instance at 0x02A99B98>4,使用对象的方法:1)class Person:
def sayHi(self):
print 'Hello,how are you?' p=Person()
p.sayHi()2)使用__init__方法class Person:
def __init__(self,name):
self.name=name def sayHi(self):
print 'Hello,my name is',self.name p=Person('John')
p.sayHi()3)使用类与对象的变量
class Person:
'''Represents a person.'''
population=0
def __init__(self,name):
'''Initializes the person's data.'''
self.name=name
print '(Initializing %s)'%self.name
Person.population +=1 def __del__(self):
'''I'm dying.'''
print '%s says bye.'%self.name
Person.population -=1
if Person.population==0:
print '''I'm the last one.'''
else:
print 'There are still %d people left.'%Person.population def sayHi(self):
'''Greeting by the person. Really,that's all it does.'''
print 'Hello,my name is%s.'%self.name def howMany(self):
'''Prints the current population.'''
if Person.population==1:
print '''I'm the only one here.'''
else:
print 'We have %d person here.'%Person.population p=Person('John')
p.sayHi()
p.howMany() p1=Person('John1')
p1.sayHi()
p1.howMany() p.sayHi()
p.howMany()属性参考:使用self变量来参考同一个对象的变量和方法。
二、继承
1,继承是用来描述类之间的类型和子类型关系。2,多态现象:一个子类型在任何需要父类型的场合可以替换父类型,即对象可以被视作父类的实例。3,基本类(超类)和子类(导出类)4,示例:class SChoolMember: #教师与学生的公有属性,基类
'''Represents any SChoolMember.'''
def __init__(self,name,age):
'''Initializes the person's data.'''
self.name=name
self.age=age
print '(Initializing SChoolMember %s)'%self.name def tell(self):
'''Tell my details.'''
print '''Name:%s\nAge:%s'''%(self.name,self.age) class Teacher(SChoolMember): #教师子类
'''Represents any Teacher.'''
def __init__(self,name,age,salary):
'''Initializes the person's data.'''
SChoolMember.__init__(self,name,age) #继承
self.salary=salary
print '(Initializing Teacher %s)'%self.name def tell(self):
'''Tell my details.'''
SChoolMember.tell(self) #继承
print '''Salary:%d'''%self.salary class Student(SChoolMember): #学生子类
'''Represents any Student.'''
def __init__(self,name,age,marks):
'''Initializes the person's data.'''
SChoolMember.__init__(self,name,age) #继承
self.marks=marks
print '(Initializing Student %s)'%self.name def tell(self):
'''Tell my details.'''
SChoolMember.tell(self) #继承
print '''Marks:%d'''%self.marks t=Teacher('Mrs.Jhon',40,4000)
s=Student('zhangbc',23,90)
members=[t,s]
for member in members:
member.tell()
print ''
三、小结
Python学习系列(八)( 面向对象基础)的更多相关文章
- Python学习系列之面向对象
概述 一.Python编程方式 面向过程编程:根据业务逻辑从上到下磊代码 面向函数编程:将某功能代码封装到函数中,将来直接调用即可,无需重新写 面向对象编程:对函数进行分类.封装 二.面向过程编程 w ...
- Python学习系列(二)(基础知识)
Python基础语法 Python学习系列(一)(基础入门) 对于任何一门语言的学习,学语法是最枯燥无味的,但又不得不学,基础概念较繁琐,本文将不多涉及概念解释,用例子进行相关解析,适当与C语言对比, ...
- Python学习系列(九)(IO与异常处理)
Python学习系列(九)(IO与异常处理) Python学习系列(八)( 面向对象基础) 一,存储器 1,Python提供一个标准的模块,称为pickle,使用它既可以在一个文件中存储任何Pytho ...
- Python学习系列(五)(文件操作及其字典)
Python学习系列(五)(文件操作及其字典) Python学习系列(四)(列表及其函数) 一.文件操作 1,读文件 在以'r'读模式打开文件以后可以调用read函数一次性将文件内容全部读出 ...
- Python学习系列(四)(列表及其函数)
Python学习系列(四)(列表及其函数) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) Python学习系列(三)(字符串) 一.基本概念 1,列表是什么? ...
- Python学习系列(三)(字符串)
Python学习系列(三)(字符串) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) 一个月没有更新博客了,最近工作上有点小忙,实在是没有坚持住,丢久又有感觉写的必要了 ...
- python学习之路-day2-pyth基础2
一. 模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...
- Django学习系列之Form基础
Django学习系列之Form基础 2015-05-15 07:14:57 标签:form django 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追 ...
- Python学习一:序列基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7858473.html 邮箱:moyi@moyib ...
随机推荐
- 1.spring cloud eureka server配置
IDEA版本 2017.2.5 JDK 1.8 红色加粗内容为修改部分 1.创建一个新项目 2.选择eureka依赖 3.版本选择(重要)并且更新依赖 <?xml version="1 ...
- 快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 247781 Accepted: 44015 Descr ...
- 项目提交到github的忽略文件
1.在工作区的.gitignore文件中配置如下: # Built application files*.apk*.ap_ # Files for the Dalvik VM*.dex # Java ...
- VS2005 使用体验
鄙人记性真心不好,看了就忘.此文记录下日常小工具的tips. 1)VS的小番茄: 破解版 Visual.Assist.X.V10.6.1833支持VS2010 VS2008 VS2005 VC6 破解 ...
- 2017-2018-2 20165202 实验四《Android程序设计》实验报告
一.实验报告封面 二.实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握Android中事件处理机制. ...
- L171
As much as we thirst for approval, we dread condemnation.我们渴望赞许,同样也害怕受到指责.If somebody's father was o ...
- 在Vim中使用gtags
之前一直使用vim+ctags+cscope来弄c的代码,最近看同事使用gtags,觉得在搜索方面要高级很多,网上大多都是emacs+gtags的资料,而vim的则比较少,这里搞通了之后,做个记录. ...
- iOS 阶段学习第四天笔记(循环)
iOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ } 两种 2.单分支 if表达式成立则执行{ }里的语句:双 ...
- 使用tr1的bind函数模板
最近把公司的VS2008统一升级为SP1了,虽然还是有些跟不上时代,毕竟C++17标准都出了,但是,对于成熟的商业软件开发而言,追求更新的C++标准肯定不是正道.升级SP1的VS2008可以支持TR1 ...
- 文件的copy
def mycopy(src_filename, dst_filename): try: fr = open(src_filename, "rb") try: try: fw = ...