这是我的代码:
class Product:
def __init__(self,id, title, price):
self.__id = id
self. __title = title
self. __price = price
def get_id_title_price(self):
return "ID: "+str(self.__id)+" Title:"+self.__title+ "Price: "+str(self.__price)
#write your code here
class Book(Product):
def __init__(self,id, title, price, isbn, publisher):
super().__init__(id, title, price)
self.isbn = isbn
self.publisher = publisher
def printDetail(self):
return f"{super().get_id_title_price()}\nISBN: {self.isbn} Publisher: {self.publisher}"
class CD(Product):
def __init__(self, id, title, price, band, duration, genre):
super().__init__(id, title, price)
self.band = band
self.genre = genre
self.duration = duration
def printDetail(self):
return "ID: "+str(self.__id)+" Title:"+self.__title+ " Shotto Price: "+str(self.__price) + "Band: "+ self.band + "Duration: "+str(self.duration)+ "Genre: "+ self.genre
book = Book(1,"The Alchemist",500,"97806","HarperCollins")
print(book.printDetail())
print("-----------------------")
cd = CD(2,"Shotto",300,"Warfaze",50,"Hard Rock")
print(cd.printDetail())
当我运行它时出现此错误:
AttributeError: 'CD' 对象没有属性 '_CD__id'
但是我找不到哪里出错了