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学习系列(八)( 面向对象基础)的更多相关文章

  1. Python学习系列之面向对象

    概述 一.Python编程方式 面向过程编程:根据业务逻辑从上到下磊代码 面向函数编程:将某功能代码封装到函数中,将来直接调用即可,无需重新写 面向对象编程:对函数进行分类.封装 二.面向过程编程 w ...

  2. Python学习系列(二)(基础知识)

    Python基础语法 Python学习系列(一)(基础入门) 对于任何一门语言的学习,学语法是最枯燥无味的,但又不得不学,基础概念较繁琐,本文将不多涉及概念解释,用例子进行相关解析,适当与C语言对比, ...

  3. Python学习系列(九)(IO与异常处理)

    Python学习系列(九)(IO与异常处理) Python学习系列(八)( 面向对象基础) 一,存储器 1,Python提供一个标准的模块,称为pickle,使用它既可以在一个文件中存储任何Pytho ...

  4. Python学习系列(五)(文件操作及其字典)

    Python学习系列(五)(文件操作及其字典) Python学习系列(四)(列表及其函数) 一.文件操作 1,读文件      在以'r'读模式打开文件以后可以调用read函数一次性将文件内容全部读出 ...

  5. Python学习系列(四)(列表及其函数)

    Python学习系列(四)(列表及其函数) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) Python学习系列(三)(字符串) 一.基本概念 1,列表是什么?     ...

  6. Python学习系列(三)(字符串)

    Python学习系列(三)(字符串) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) 一个月没有更新博客了,最近工作上有点小忙,实在是没有坚持住,丢久又有感觉写的必要了 ...

  7. python学习之路-day2-pyth基础2

    一.        模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...

  8. Django学习系列之Form基础

     Django学习系列之Form基础 2015-05-15 07:14:57 标签:form django 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追 ...

  9. Python学习一:序列基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7858473.html 邮箱:moyi@moyib ...

随机推荐

  1. nginx模块记录

    1. ngx_http_ssl_module 让Nginx可以支持HTTPS的模块 (1)ssl on | off; #是否开启ssl功能 (2)ssl_certificate file; #当前虚拟 ...

  2. wordcount程序出现map 100% reduce 0%问题的解决方法

    运行wordcount程序一直停在map 100% reduce 0%, input文件夹的内容: 其中: f1.txt中的内容为:hello hadoop f2.txt中的内容为:hello had ...

  3. 转:CentOS 7使用nmcli配置双网卡聚合LACP

    进入CentOS 7以后,网络方面变化比较大,例如eth0不见了,ifconfig不见了,其原因是网络服务全部都由NetworkManager管理了,下面记录下今天下午用nmcli配置的网卡聚合,网络 ...

  4. Oracle 反应太后知后觉了.

    很久已经提过一个SR,关于BES一个用户可以用两个密码登陆EBS系统的问题,但是SR解决太慢,而且一致强调你们的版本太低,需要升级到最新的版本,考虑客户化的问题,我们的版本没有升级(R2.1.1),无 ...

  5. leetcode 849. Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  6. 华为EPON OLT开局配置

      配置思路: 1. 登录olt(console进去之后配地址) 2.配置上联口(配vlan和起三层地址互联路由的lan口) 3.epon接分光器,分光器下接光猫 4.自动发现光猫.配置DBA数据和线 ...

  7. 轮播图插件 SuperSlide2.1滑动门jQuery插件

    http://down.admin5.com/demo/code_pop/18/562/ SuperSlide2.1滑动门jQuery插件

  8. LINUX中文件描述符传递

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  9. python金融分析项目

    1.进入ipython: C:\Users\Administrator>ipython Python (v3. , ::) [MSC v. bit (AM D64)] Type 'copyrig ...

  10. 【转】netlink socket编程实例

    [转]netlink socket编程实例 转自:http://blog.chinaunix.net/uid-14753126-id-2983915.html 关于Netlink IPC方式的介绍,请 ...