设为首页 收藏本站
查看: 901|回复: 0

[经验分享] Python学习系列(八)( 面向对象基础)

[复制链接]
累计签到:11 天
连续签到:1 天
发表于 2015-12-2 09:32:46 | 显示全部楼层 |阅读模式
Python学习系列(八)( 面向对象基础)

Python学习系列(七)( 数据库编程)



一,面向对象




1,域:属于一个对象或类的变量。有两种类型,即实例变量—属于每个实例/类的对象;类变量—属于类本身。

2,类的方法:对象也可以使用属于类的函数来具有功能,这样的函数称之为类的方法。域和方法合称为类的属性。类使用class关键字创建,类的属性被列在一个缩进块中。

3,self:类的方法与普通的函数只有一个特别的区别----他们必须有一个额外的第一个参数名称,但是在调用的时候不能为其赋值,Python会提供。这个特别的变量指对象本身,按照惯例命名为self。self等价于C++中的self指针和Java,C#中的this。

创建一个类:



DSC0000.gif DSC0001.gif


1 class Person:
2     pass
3  
4 p=Person()
5 print p
6 >>> ============= RESTART ================================
7 >>>
8 <__main__.Person instance at 0x02A99B98>
View Code

4,使用对象的方法:

1)






1 class Person:
2     def sayHi(self):
3         print 'Hello,how are you?'
4  
5 p=Person()
6 p.sayHi()
View Code

2)使用__init__方法






1 class Person:
2     def __init__(self,name):
3         self.name=name
4        
5     def sayHi(self):
6         print 'Hello,my name is',self.name
7  
8 p=Person('John')
9 p.sayHi()
View Code  3)使用类与对象的变量







1 class Person:
2     '''Represents a person.'''
3     population=0
4     def __init__(self,name):
5         '''Initializes the person's data.'''
6         self.name=name
7         print '(Initializing %s)'%self.name
8         Person.population +=1
9        
10     def __del__(self):
11         '''I'm dying.'''
12         print '%s says bye.'%self.name
13         Person.population -=1
14         if Person.population==0:
15             print '''I'm the last one.'''
16         else:
17             print 'There are still %d people left.'%Person.population
18            
19     def sayHi(self):
20         '''Greeting by the person.
21  
22         Really,that's all it does.'''
23         print 'Hello,my name is%s.'%self.name
24  
25     def howMany(self):
26         '''Prints the current population.'''
27         if Person.population==1:
28             print '''I'm the only one here.'''
29         else:
30             print 'We have %d person here.'%Person.population
31  
32   
33 p=Person('John')
34 p.sayHi()
35 p.howMany()
36  
37 p1=Person('John1')
38 p1.sayHi()
39 p1.howMany()
40  
41 p.sayHi()
42 p.howMany()
View Code  属性参考:使用self变量来参考同一个对象的变量和方法。


二、继承


1,继承是用来描述类之间的类型和子类型关系。

2,多态现象:一个子类型在任何需要父类型的场合可以替换父类型,即对象可以被视作父类的实例。

3,基本类(超类)和子类(导出类)

4,示例:






1 class SChoolMember: #教师与学生的公有属性,基类
2     '''Represents any SChoolMember.'''
3     def __init__(self,name,age):
4         '''Initializes the person's data.'''
5         self.name=name
6         self.age=age
7         print '(Initializing SChoolMember %s)'%self.name
8  
9     def tell(self):
10         '''Tell my details.'''
11         print '''Name:%s\nAge:%s'''%(self.name,self.age)
12  
13 class Teacher(SChoolMember): #教师子类
14     '''Represents any Teacher.'''
15     def __init__(self,name,age,salary):
16         '''Initializes the person's data.'''
17         SChoolMember.__init__(self,name,age) #继承
18         self.salary=salary
19         print '(Initializing Teacher %s)'%self.name
20  
21     def tell(self):
22         '''Tell my details.'''
23         SChoolMember.tell(self) #继承
24         print '''Salary:%d'''%self.salary
25  
26 class Student(SChoolMember): #学生子类
27     '''Represents any Student.'''
28     def __init__(self,name,age,marks):
29         '''Initializes the person's data.'''
30         SChoolMember.__init__(self,name,age) #继承
31         self.marks=marks
32         print '(Initializing Student %s)'%self.name
33  
34     def tell(self):
35         '''Tell my details.'''
36         SChoolMember.tell(self) #继承
37         print '''Marks:%d'''%self.marks
38  
39  
40 t=Teacher('Mrs.Jhon',40,4000)
41 s=Student('zhangbc',23,90)
42 members=[t,s]
43 for member in members:
44     member.tell()
45     print ''
View Code

三、小结

      时隔快一年了,动笔生疏了,想想还是学习比较踏实吧。本篇幅较短,重在理解,后续逐渐完善。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-146155-1-1.html 上篇帖子: Python学习笔记7-高级迭代器 下篇帖子: Python 技巧
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表