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()

 

转载于:https://www.cnblogs.com/passion-hzhang/p/5850410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值