• 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()
  • 相关阅读:
    java环境变量配置
    经纬度与度分秒转换
    Linux下屏幕截图
    mCustomScrollbar动态加载滚动条
    解决PHPExcel列超过26的问题
    解决PHPExcel长数字串显示为科学计数
    URL获取并修改参数【转】
    Windows下启动nginx,关闭nginx
    Linux 查看目录的相关权限
    Python文件装换成C文件,主要用于隐藏核心文件实现步骤
  • 原文地址:https://www.cnblogs.com/passion-hzhang/p/5850410.html
Copyright © 2020-2023  润新知