热线电话:13121318867

登录
2019-02-20 阅读量: 604
如何理解python类方法的多态性

我们创建一个for循环,遍历一个对象元组。然后调用函数而不关心每个对象是哪个类类型。我们假设这些方法实际上存在于每个类中。

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.")

obj_ind = India()

obj_usa = USA()

for country in (obj_ind, obj_usa):

country.capital()

country.language()

country.type()

输出:

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.
0.0000
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子