python 中物件導向編程的幾個概念

來源:互聯網
上載者:User

標籤:python2   fun   name   not   col   odi   har   style   html   

  • Python super() 函數

super() 函數是用於調用父類(超類)的一個方法。

super 是用來解決多重繼承問題的,直接用類名調用父類方法在使用單繼承的時候沒問題,但是如果使用多繼承,會涉及到尋找順序(MRO)、重複調用(鑽石繼承)等種種問題。

MRO 就是類的方法解析順序表, 其實也就是繼承父類方法時的順序表。

Python3.x 和 Python2.x 的一個區別是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

Python3.x 執行個體:

class A:    passclass B(A):    def add(self, x):        super().add(x)

Python2.x 執行個體:

class A(object):   # Python2.x 記得繼承 object    passclass B(A):    def add(self, x):        super(B, self).add(x)
#!/usr/bin/python# -*- coding: UTF-8 -*- class FooParent(object):    def __init__(self):        self.parent = ‘I\‘m the parent.‘        print (‘Parent‘)        def bar(self,message):        print ("%s from Parent" % message) class FooChild(FooParent):    def __init__(self):        # super(FooChild,self) 首先找到 FooChild 的父類(就是類 FooParent),然後把類B的對象 FooChild 轉換為類 FooParent 的對象        super(FooChild,self).__init__()            print (‘Child‘)            def bar(self,message):        super(FooChild, self).bar(message)        print (‘Child bar fuction‘)        print (self.parent) if __name__ == ‘__main__‘:    fooChild = FooChild()    fooChild.bar(‘HelloWorld‘)
  • 多重繼承的時候怎麼指定父類???
  • super()的好處就是可以避免直接使用父類的名字.但是它主要用於多重繼承,這裡面有很多好玩的東西.如果還不瞭解的話可以看看官方文檔
  • python子類如何調用父類的__init__方法

Python不會自動調用父類的constructor,你得親自專門調用它。

兩種方法:

    1. 父類名.__init__(self,參數) #注意名字是父類    
    2. super(子類名,self).__init__(參數) #注意名字是子類,而且init後是self之外的參數 
# coding=utf-8  class Person(object):      def __init__(self, name="jim"):          self.name = name          self.flag = False          print "Person",self.name        def call(self):          print self.flag,"name:",self.name          self.flag = not self.flag    class Programmer(Person):      def __init__(self):          Person.__init__(self,"Dotjar")   #第一種方法        print "Programmer"        def set_name(self,name):          self.name=name    coder = Programmer()  coder.call()  coder.set_name("dotjar")  coder.call()  
# coding=utf-8  class Person(object):      def __init__(self, flag=False, name="jim"):          self.name = name          self.flag = flag          print "Person",self.name        def call(self):          print self.flag,"name:",self.name          self.flag = not self.flag    class Programmer(Person):      def __init__(self, flag = True, name="Dotjar",age = 19):          self.age = 19          super(Programmer,self).__init__(flag,name)  # 第二種方法        print "Programmer‘s age:",self.age        def set_name(self,name):          self.name=name  
  • Python中抽象類別和介面的區別

https://code.i-harness.com/zh-CN/q/5ad4a

python 中物件導向編程的幾個概念

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.