【问题标题】:How to connect different classes together?如何将不同的类连接在一起?
【发布时间】:2020-01-28 09:02:08
【问题描述】:

我想制作一个程序来制作有关音乐专辑的信息:专辑中的标题、艺术家和歌曲。关于艺术家,名字或名字(如果是多于一位艺术家)应该被注册。对于歌曲,歌曲名称,如果需要,应该注册作曲家的名字。 该程序应该有专辑、艺术家和歌曲的课程。艺术家的类有一个函数,如果艺术家是独奏艺术家,则返回“真”,否则返回“假”。每张专辑都有一个从 1 开始的序列号,按照它们在系统中添加的顺序。 Album 的类具有返回序列号的函数。

最后,程序应该具有遍历专辑列表的功能,并打印指定艺术家的专辑名称和歌曲名称。如果没有给出艺术家,则将打印整个列表。

我尝试了一些,但我不明白如何连接类,以及如何制作一个可以保存所有数据的列表。

class Artists:


    def __init__(self, artist_name):
        self.artist_name = artist_name


        artist_list = {}
        artist_list.append(artist_name)

        if range(artist_list)==1: print("True")
        else: print("False")


class Album:
    totalAlbums=0 #for automatically generate serial numbers
    serialnumber = None

    def __init__(self, albumtitle, artist, serialnumber):
        Album.totalAlbums=Album.totalAlbums+1
        self.serienummer=Album.totalAlbums
        self.albumtitle=albumtitle
        self.artist=artist
        self.serialnumber=serialnumber

        self.songs = {}

        artist.add_song(self)


class Song:

        def __init__(self, songtitle, composer):
            self.songtitle=sangtitle
            self.composer=composer

for artist in albumlist: print()

【问题讨论】:

  • 您的问题对于 SO 的问答格式而言过于宽泛。如果你想得到答案,你应该把它限制在更窄、更好定义的范围内。

标签: python function class attributes


【解决方案1】:

您正处于您正在处理的项目的“需求”阶段。


此时您可以对自己的想法进行伪编码,但正如其他 cmets 所建议的那样,实际代码仍然需要更多要求。


我建议此时检查您的假设,并了解数据中多对多关系的数据完整性。


IE


一张专辑可以有很多作曲家,就像一首歌一样。 在您的写作中,您似乎假设每首歌曲都有一位且只有一位作曲家。


需求的高级定义


https://en.wikipedia.org/wiki/Requirement


需求的软件定义


https://en.wikipedia.org/wiki/Software_requirements_specification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多