python基础教程之抽象

 

很早知道python,但没有坚持学习。最近心血来潮,但能弥补这个遗憾。

对象几个重要概念:

多态:可以对不同类的对象使用同样的操作;

封装:对外部世界隐藏对象的工作细节;

继承:以普通的类为基础建立专门的类对象。

 

继承举例:

class AddrBookEntry(object):
'address book entry class'
def __init__(self, nm, ph):
self.name = nm
self.phone = ph
print 'created instance for:', self.name

def updatePhone(self, newph):
self.phone = newph
print 'Updated phone# for:', self.name

子类:
class EmplAddrBookEntry(AddrBookEntry):
'Employee address book entry class'
def __init__(self, nm, ph, id, em):
AddrBookEntry.__init__(self, nm, ph)
self.empid = id
self.email = em

def updateEmail(self, newem):
self.email = newem
print 'updated e-mail address for:', self.email

多态举例

多态是在类实例化之后表现出来的属性, 意味着就算不知道所引用的对象类型是什么,还能对它进行操作,而它也会根据对象类型的不同而表现出不同的行为。

在继承过程中,较为隐性的,当子类重新定义父类的功能接口时,父类的功能接口将被覆盖。

class Human(object):
def run(self):
print 'human is running...'


class man(Human):
pass

class woman(Human):
pass

john = man()
jude = woman()

john.run()
jude.run()

class man1(Human):
def run(self):
print 'man is running...'
pass

class woman1(Human):
def run(self):
print 'woman is running...'
pass

john = man1()
jude = woman1()

john.run()
jude.run()

 

posted on 2016-09-07 18:04  小生有悔  阅读(183)  评论(0编辑  收藏  举报

导航