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()
  • 相关阅读:
    (3.4)常用知识-char与varchar的选择
    (3.3)常用知识-索引使用、维护与填充因子
    (3.2)常用知识-字符串处理
    (3.1)常用知识-日期处理
    基于Sql Server 2008的分布式数据库的实践
    将expression转化为数据类型int时发生算术溢出错误
    webserver and application server
    web前端
    linkin
    jquery load
  • 原文地址:https://www.cnblogs.com/passion-hzhang/p/5850410.html
Copyright © 2011-2022 走看看