为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确初始化一个类

如何正确初始化一个类

PHP
慕森王 2023-11-09 15:43:36
对于我的 python 作业,我必须创建类并初始化它们。我正确创建了它们,但自动评分器说它们未正确初始化。使用初始化艺术家信息的构造函数和 print_info() 方法定义 Artist 类。默认情况下,构造函数应将艺术家的姓名初始化为“None”,并将出生年份和死亡年份初始化为 0。 print_info() 应显示艺术家姓名,如果死亡年份为 -1,则应显示出生 XXXX,否则应显示艺术家姓名 (XXXX-YYYY) 。使用构造函数定义 Artwork 类以初始化艺术品的信息和 print_info() 方法。默认情况下,构造函数应将标题初始化为“None”,创建年份为 0,并且艺术家应使用 Artist 默认构造函数参数值。到目前为止我所拥有的:class Artist:   def __init__(self, user_artist_name="None", user_birth_year=0, user_death_year=0):       self.name = user_artist_name       self.birth = user_birth_year       self.death = user_death_year   def print_info(self):       if self.death == -1:           print("Artist: {}, born {}".format(self.name, self.birth))       else:           print("Artist: {} ({}-{})".format(self.name, self.birth, self.death))class Artwork:   def __init__(self, user_title= "None", year_created=0, user_artist=Artist()):       self.title = user_title       self.year = year_created       self.artist = user_artist   def print_info(self):       print("Title: {}, {}".format(self.title, self.year))if __name__ == "__main__":   user_artist_name = input()   user_birth_year = int(input())   user_death_year = int(input())   user_title = input()   user_year_created = int(input())   user_artist = Artist(user_artist_name, user_birth_year, user_death_year)      user_artist.print_info()   new_artwork = Artwork(user_title, user_year_created, user_artist)   new_artwork.print_info()Artist('Pablo Picasso', 1881, 1973) 未能正确初始化艺术家。并且 Artist 和 Artwork 的默认参数的构造函数都失败。我缺少什么?
查看完整描述

3 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

看起来该练习经历了多个变量更新,并且他们忘记更新许多初始值的模板。无论如何,这应该可以正常工作。


main.py

from Artist import Artist

from Artwork import Artwork


if __name__ == "__main__":

    user_artist_name = input()

    user_birth_year = int(input())

    user_death_year = int(input())

    user_title = input()

    user_year_created = int(input())


    user_artist = Artist(user_artist_name, user_birth_year, user_death_year)


    new_artwork = Artwork(user_title, user_year_created, user_artist)


    new_artwork.print_info()

Artist.py

class Artist:

def __init__(self, name="None", birth_year=0, death_year=0):

    self.name = name

    self.birth_year = birth_year

    self.death_year = death_year


def print_info(self):

    if self.death_year == -1:

        print('Artist: {}, born {}'.format(self.name, self.birth_year))

    else:

        print('Artist: {} ({}-{})'.format(self.name, self.birth_year, self.death_year))

Artwork.py

from Artist import Artist 


class Artwork:

def __init__(self, user_title="None", year_created=0, user_artist=Artist()):

    self.title = user_title

    self.year_created = year_created

    self.artist = user_artist


def print_info(self):

    self.artist.print_info()

    print('Title: %s, %d' % (self.title, self.year_created))


查看完整回答
反对 回复 2023-11-09
?
慕容708150

TA贡献1831条经验 获得超4个赞

这就是我得到的。


class Artist:

    def __init__(self, name = 'None', birth_year = 0.0, death_year = 0.0):

        self.name = name

        self.birth_year = birth_year

        self.death_year = death_year

    def print_info(self):

        if self.death_year == -1:

            print('Artist: {}, born {}'.format(self.name, self.birth_year))

        else:

            print('Artist: {} ({}-{})'.format(self.name, self.birth_year, self.death_year))

  

class Artwork:

    def __init__(self, title = 'None', year_created = 0, artist = Artist()):

        self.title = title

        self.year_created = year_created

        self.artist = artist

    def print_info(self):

        self.artist.print_info()

        print('Title: {}, {}'.format(self.title, self.year_created))


if __name__ == "__main__":

    user_artist_name = input()

    user_birth_year = int(input())

    user_death_year = int(input())

    user_title = input()

    user_year_created = int(input())


    user_artist = Artist(user_artist_name, user_birth_year, user_death_year)


    new_artwork = Artwork(user_title, user_year_created, user_artist)

  

    new_artwork.print_info()


查看完整回答
反对 回复 2023-11-09
?
翻阅古今

TA贡献1780条经验 获得超5个赞

'''


class Artist:

    def __init__(self, name=str(None), birth_year=0, death_year=0):

        self.name = name

        self.birth_year = birth_year

        self.death_year = death_year


    def print_info(self):

        if self.death_year < 0:

           print (f'Artist: {self.name}, born {self.birth_year}')

        else:

           print (f'Artist: {self.name} ({self.birth_year}-{self.death_year})')

 

class Artwork:

    def __init__(self, title=str(None), year_created=0, artist=Artist()):

        self.title = title

        self.year_created = year_created

        self.artist = artist

    

    def print_info(self):

        self.artist.print_info()

        print (f'Title: {self.title}, {self.year_created}')


if __name__ == "__main__":

    user_artist_name = input()

    user_birth_year = int(input())

    user_death_year = int(input())

    user_title = input()

    user_year_created = int(input())


    user_artist = Artist(user_artist_name, user_birth_year, user_death_year)

    new_artwork = Artwork(user_title, user_year_created, user_artist)


    new_artwork.print_info()

'''


查看完整回答
反对 回复 2023-11-09
  • 3 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信