python类继承理解

python类继承理解,第1张

概述我想更好地了解如何使用 python类继承. 我在网上发现了以下问题. Include a Furnishing class. During instantiation, this class should ask for a room argument. Then, create the following classes that inherit from the Furnishing cla 我想更好地了解如何使用 python类继承.

我在网上发现了以下问题.

Include a Furnishing class. During instantiation,this class should ask for a room argument. Then,create the following classes that inherit from the Furnishing class: Sofa,Bookshelf,bed,and table.

Use the built-in List type to record the furniture in your own home that matches the classes above. So for example,you might have:

>>> from furnishings import * >>> home = [] >>> home.append(bed('bedroom'))>>> home.append(Sofa('living Room'))

Now,write a map_the_home() function to convert that to a built-in dict type where the rooms are the indivIDual keys and the associated value is the List of furniture for that room. If we ran that code against what we display in our command line,we might see:

>>> map_the_home(home){'bedroom': [<__main__.bed object at 0x39f3b0>],'living Room':[<__main__.sofa object at 0x39f3b0>]}

我正在努力:

class Furnishing(object):    def __init__(self,room):        self.room = roomclass Sofa(Furnishing):    "Not sure how to get this into a dict"???

我不知道如何调用map_to_home(home)并让它返回所需的字典?

解决方法 它会像以下一样简单:

def map_the_home(home):    result = dict([(a.room,a) for a in home])    return result

不是吗?

总结

以上是内存溢出为你收集整理的python类继承理解全部内容,希望文章能够帮你解决python类继承理解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1197051.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存