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 ...
随机推荐
- 二十一 Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫数据保存
注意:数据保存的操作都是在pipelines.py文件里操作的 将数据保存为json文件 spider是一个信号检测 # -*- coding: utf-8 -*- # Define your ite ...
- HDU-4355-三分
Party All the Time Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- yii2的定时任务
php yii minsheng-cancel-account/cancel-applied-account
- MySQL修改字符集编码
通过修改字符集编码为utf8,彻底解决中文问题. 一. 登录MySQL查看用SHOW VARIABLES LIKE 'character%':下字符集,显示如下: +----------------- ...
- webapi返回不带引号的字符串,解决自动加双引号的问题
返回类型改为HttpResponseMessage类型 [Route("api/TestControllers/test")] [HttpGet] public HttpRespo ...
- LeetCode OJ:Rotate List(旋转链表)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 修复 海盗船 k70 lux 未检测到设备(k70 no device detected)
corsair k70 lux 上周收到的生日礼物,头一次用机械键盘,还是这么高端的机械键盘(729RMB),手感一级棒.但是,有问题啊!把键盘上的 bios按钮拨到8上电脑可以识别,scroll 灯 ...
- python中sort()与sorted()的区别
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...
- 终于也忍不住来写oi经历了
感觉好绝望. 突然间觉得这么长时间的oi学了就像没学一样,这么多的题做了就像没做一样. 努力付出,却不知希望在何处,也不知道该怎么办. 我好丧啊. 但是又没有办法 既然当初选择oi这条路 就只能继续走 ...
- # 2018-2019-2 20165210《网络攻防技术》Exp1 PC平台逆向破解(BOF实验)
2018-2019-2 20165210<网络攻防技术>Exp1 PC平台逆向破解(BOF实验) 实验分为三个部分: 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. ...