類的繼承、類的派生、類的組合


類的繼承

  • 子類繼承父類的所有內容,可以繼承多個
class ParentClass1:
	pass

class ParentClass2:
    pass

class SubClass(ParentClass1, ParentClass2):
    pass

類的派生

  • 派生:在繼承的基礎上,子類會添加屬於自己的屬性
class ParentClass1:
    def __init__(self, name, age):
        self.name = name
        self.age = age


class ParentClass2(ParentClass1):
    def __init__(self, name, age, height):
        super(ParentClass2, self).__init__(name, age)
        self.height = height


class SubClass1(ParentClass2):  # 普通的繼承
    pass


class SubClass2(ParentClass2):  # 如果是多繼承,查找順序,按照mro算法
    def __init__(self, gender, name, age, height):
        # ParentClass1.__init__(self, name, age)
        super().__init__(name, age, height)
        self.gender = gender  # 派生


sc = SubClass2('male', 'nick', 18, 180)  # 實例化的時候自動調用__init__

類的組合

  • 把類對象當作變量值/參數/返回值/容器元素使用,間接的使用類對象的方法
class F1:
    count =0
    pass


class F2:
    count = 1
    pass


# f1 = F1()
# f2 = F2()
# f1.f2 = f2  # 組合
# print(f1.f2.count)

f1 = F1()
f2 = F2()
print(f2.__dict__)
f2.f1 = f1
print(f2.__dict__)
# f2.f1(key) = f1(value)
print(f2.__dict__['f1'].count)
print(f2.f1.count)

def f3():
    return f1

f = f3()  # f = f1
print(f.count)

def f4():
    return F1

f = f4()
print(f().count)

菱形繼承問題

  • 繼承多個父類,並且最終匯集到一個父類

新式類

  • 繼承了object類的類,Python3中只有新式類
  • 廣度優先:老祖宗最后查

經典類

  • 沒有繼承object的類,只有Python2中有
  • 深度優先:一路走到底,然后再找繼承的第二個
class G(object):
    # def test(self):
    #     print('from G')
    pass


print(G.__bases__)


class E(G):
    # def test(self):
    #     print('from E')
    pass


class B(E):
    # def test(self):
    #     print('from B')
    pass


class F(G):
    # def test(self):
    #     print('from F')
    pass


class C(F):
    # def test(self):
    #     print('from C')
    pass


class D(G):
    # def test(self):
    #     print('from D')
    pass


class A(B, C, D):
    def test(self):
        print('from A')


obj = A()
for i in A.__mro__:
    print(i)
    
'''
(<class 'object'>,)
<class '__main__.A'>
<class '__main__.B'>
<class '__main__.E'>
<class '__main__.C'>
<class '__main__.F'>
<class '__main__.D'>
<class '__main__.G'>
<class 'object'>
'''

多態與多態性

  • 多態就是多種形態,動物會有人/🐖/🐶
# Python本身就是多態,根本就不支持多態

class Animal():
    def eat(self):
        print('eat')

class People(Animal):
    def eat(self):
        print('人吃')
class Dog(Animal):
    def eat(self):
        print('🐶吃')
class Pig(Animal):
    def eat(self):
        print('🐖吃')
    def sleep(self):
        print('🐖睡')
class F(Animal):
    def eat(self):
        print('f吃')
f = F()

peo = People()
pig = Pig()
dog = Dog()

# 多態性
peo.eat()
pig.eat()
dog.eat()
f.eat()

class Cat(Animal):
    def eat(self):
        print('🐱吃')
cat = Cat()
cat.eat()

# 100種動物
print('*'*50)
# 多態性的使用,提供接口的概念
def func(obj):
    obj.eat()
    obj.run()
    obj.walk()
    obj.sleep()

func(cat)
# cat.eat()
# cat.walk()
# cat.run()
# cat.sleep()
func(dog)
# dog.eat()
# dog.walk()
# dog.run()
# dog.sleep()
func(peo)

# 取錢,插卡-》輸入密碼-》輸入金額-》取到錢了

# (插卡-》輸入密碼-》輸入金額-》取到錢了)--》取錢函數

# 鴨子類型:只要長得像🦆,叫的像🦆,游泳也想🦆,你就是🦆


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM