python實現多型 如何用python實現多型性

2021-10-17 02:41:02 字數 1035 閱讀 6894

建立乙個名為「func()」的函式,它將獲取乙個我們將其命名為「obj」的物件。雖然我們使用的名稱是'obj',但是任何例項化的物件都可以被呼叫到這個函式中。

**:用函式實現多型性

class india():

def capital(self):

print("new delhi is the capital of india.")

def language(self):

print("hindi the primary language of india.")

def type(self):

print("india is a developing country.")

class usa():

def capital(self):

print("washington, d.c. is the capital of usa.")

def language(self):

print("english is the primary language of usa.")

def type(self):

print("usa is a developed country.")

def func(obj):

obj.capital()

obj.language()

obj.type()

obj_ind = india()

obj_usa = usa()

func(obj_ind)

func(obj_usa)

輸出:new delhi is the capital of india.

hindi the primary language of india.

india is a developing country.

washington, d.c. is the capital of usa.

english is the primary language of usa.

usa is a developed country.

如何用python實現數字轉漢字?

在大多數情況下,漢字都比數字要顯得正式一些。比如說,二零一九年 就比較有官方檔案的味道,而 2019年 則更有個人日記的味道。另外,漢字還可以用來編繞口令!用數字的話就難得多。還是舉個栗子 14是14,40是40。就顯得很 十四是十四,四十是四十。就有一種唇齒生香的感覺。山前有44只石獅子,山後有4...

python 如何呼叫py檔案

方法1 from file in import myfunc 方法2 import file in file in.myfunc arg 函式呼叫 demo.py folder a init py file1.py現需要在demo.py中呼叫file1.py檔案,方法如下 方法1 foldera資料...

Python 如何用列表實現棧和佇列

前面學習了列表的基礎知識,本著學以致用的原則,就想著如何通過列表來實現資料結構棧和佇列。x 建立乙個空列表,此處表示棧 x x a x a b x.pop 彈出棧頂元素 b b x a x.pop 彈出棧頂元素 a a x x.pop 試圖對乙個空棧做彈出操作,會報異常 traceback most...