zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    持续集成环境搭建(一)
    The POSIX API/nss/nscd
    [postfix]转发邮件设置
    [python]python安装包错误
    栈及栈帧讲解
    kernel-init-bash
    nginx no input file specified
    Lumen/Laravel调试API接口利器laravel-debugbar
    homestead实现外部局域网络其他主机的访问
    PHP rabbitmq扩展安装
  • 原文地址:https://www.cnblogs.com/passion-hzhang/p/5850410.html
Copyright © 2011-2022 走看看