class 派生類名(基礎類別名):
<資料成員宣告 1>
...
<資料成員宣告 N>
class Animal: name = "" def Speak(self): pass class Cat(Animal): name = "狗" def Speak(self): print ("喵~喵~喵~") class Human(Animal): name ="人" def Speak(self): print ("你好~") c = Cat() c.Speak() h = Human() h.Speak()上述程式碼的執行結果如下所示:
>>> class Animal:
... name = ""
... def Speak(self):
... pass
>>> class Cat(Animal):
... name = "狗"
... def Speak(self):
... print ("喵~喵~喵~")
>>> class Human(Animal):
... name ="人"
... def Speak(self):
... print ("你好~")
>>> c = Cat()
>>> c.Speak()
喵~喵~喵~
>>> h = Human()
>>> h.Speak()
你好~
class 派生類名(基礎類別1, 基礎類別2, ......, 基礎類別N):
<資料成員宣告 1>
...
<資料成員宣告 N>
class Animal: name = "" def Speak(self): pass class Cat(Animal): name = "狗" def Speak(self): print ("喵~喵~喵~") class Human(Animal): name ="人" def Speak(self): print ("你好~") class Actor(Human, Cat): name ="演員" def Speak(self): Human.Speak(self) Cat.Speak(self) a = Actor() a.Speak()以上程式碼的執行結果如下所示:
>>> class Animal:
... name = ""
... def Speak(self):
... pass
>>> class Cat(Animal):
... name = "狗"
... def Speak(self):
... print ("喵~喵~喵~")
>>> class Human(Animal):
... name ="人"
... def Speak(self):
... print ("你好~")
>>> class Actor(Human, Cat):
... name ="演員"
... def Speak(self):
... Human.Speak(self)
... Cat.Speak(self)
>>> a = Actor()
>>> a.Speak()
你好~
喵~喵~喵~