python物件導向(下)

ZangzzzzZ發表於2020-11-17

物件導向第三講

  • python父類如果實現了構造方法,子類沒有,例項化子類物件時會預設呼叫父類的構造方法

    class G():
        def __init__(self):
            print('G __init__ invoke...')
    class P():
        def __init__(self):
            print('P __init__ invoke...')
    class A(P,G):
        pass
        #def __init__(self):
        #    P.__init__(self)
        #print('A __init__ invoke...')
    if __name__ == '__main__':
        a=A()
  • 父類方法呼叫多種方法

    class P1():
        def __init__(self):
            print('P1 __init__ invoke...')
    class P2():
        def __init__(self):
            print('P2 __init__ invoke...')
    class A(P1,P2):
        def __init__(self):
            P1.__init__(self)
            P2.__init__(self)
            print('A __init__ invoke...')
    
    if __name__ == '__main__':
        a=A()
  • 第二種方法super(A.self)

    class G():
        def __init__(self):
            print('G __init__ invoke...')
    class P():
        def __init__(self):
            print('P __init__ invoke...')
    class A(P,G):
        def __init__(self):
            #P.__init__(self)
            #G.__init__(self)
            super(A,self).__init__()
            print('A __init__ invoke...')
    if __name__ == '__main__':
        a=A()
  • 靜態方法無法訪問物件的自身屬性變數

    class A():
        method=1#靜態屬性
        def __init__(self,name):
            self.name=name
        def run(self):
            print('run invoke...')
        @staticmethod
        def func(x):
            print('self.name=%s'%x)
            print('func invoke...')
        @classmethod
        def test(cls):
            print('test...')
    if __name__ == '__main__':
        a=A('name')
        a.run()
        A.func('ZangzzzZ')
        A('xxx').func('yyy')
        A.test()
        print(dir(A.run('name')))
        print(dir(A))
        A.func('ZangzzzzZ')
  • 方法過載 str

    class A(object):
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def __str__(self):#方法過載
            return 'name=%s,age=%d'%(self.name,self.age)
    if __name__ == '__main__':
        a=A('ZangzzZ',20)
        print(str(a))#str(a)和a
  • 單連結串列結構

    #實現一個單連結串列
    
    #用node來作為連結串列的節點抽象類
    class Node():
    
        def __init__(self,data):
            self.data=data
            self.next=None
        def show(self):
            print('node data=%d'%self.data)
        def __str__(self):
            return 'node data=%s'%self.data
    class LinkList():
        def __init__(self):
            self.head=None
        def insertFirst(self,data):
            newNode = Node(data)#建立新節點
            newNode.next=self.head
            self.head = newNode
        def deleteFirst(self):
            tmpNode= self.head
            self.head=self.head.next
            return tmpNode
        def travel(self):
            currentNode = self.head
            while currentNode is not None:
                print(currentNode)
                currentNode = currentNode.next
    if __name__ == '__main__':
        n1=Node('A')
        n2=Node('B')
        n3=Node('C')
        link=LinkList()
        link.insertFirst(n1)
        link.insertFirst(n2)
        link.insertFirst(n3)
        link.travel()
        print('+'*30)
        link.deleteFirst()
        link.deleteFirst()
        link.deleteFirst()
        link.travel()
  • 過載__iter__和__next__實現一個可迭代的型別(有限迴圈的版本)

    import random
    class A():
        def __init__(self,seq):
            '''seq type(iterable)'''
            self.seq=seq
            self.index=0
        def __iter__(self):
            return self#返回當前物件
        def __next__(self):
            if self.index > len(self.seq) - 1:
                raise StopIteration
            else:
                tmp = self.seq[self.index]
                self.index+=1
                return tmp
    if __name__ == '__main__':
        a=A([1,2,3,50,43])
        for item in a:
            print(item)
  • 過載__iter__和__next__實現一個可迭代的型別(無限迴圈的版本)

    import random
    class A():
        def __init__(self,seq):
            '''seq type(iterable)'''
            self.seq=seq
        def __iter__(self):
            return self#返回當前物件
        def __next__(self):
            return self.seq[random.randint(0,len(self.seq)-1)]
    if __name__ == '__main__':
        a=A([1,2,3])
    
        for item in a:
            print(item)

相關文章