這是我學校的代碼請幫助
class product:
deliveryCharge=50
def __init__(self,nam="Teddy Bear", prc=500):
self.name=nam
self.price=prc
def get_name(self):
return self.name
def get_price(self):
return self.price product.deliveryCharge
def __str__(self):
return "The {} will cost you Rs.{}.".format(self.get_name(),self.get_price())
class gift(product):
def __init__(self,nam,prc,wrpchrge=100):
super().__init__(nam,prc)
self.wrappingcharge=wrpchrge
def get_price(self):
return self.price product.deliveryCharge gift.wrappingcharge
x1=product("yoyo",29)
print("I am buying a {} and it costs me {}".format(x1.get_name,x1.get_price))
m1=gift("yoyo",29)
print("I am buying a {} and it costs me {}".format(m1.get_name,m1.get_price))
print(product.get_name)
我得到的錯誤:

如果有人知道如何解決這個問題,
uj5u.com熱心網友回復:
您正在列印對記憶體中物件的參考。使用括號列印實際值。例如x1.get_name應該是x1.get_name(). 還有其他問題,你需要使用 self 來參考兩個類中的值,即使是在產品中的值的禮品類中,因為它繼承了它的功能。
這是一個例子:
class product:
deliveryCharge=50
def __init__(self,nam="Teddy Bear", prc=500):
self.name=nam
self.price=prc
def get_name(self):
return self.name
def get_price(self):
return self.price self.deliveryCharge # changed from product to self
def __str__(self):
return "The {} will cost you Rs.{}.".format(self.get_name(),self.get_price())
class gift(product):
def __init__(self,nam,prc,wrpchrge=100):
super().__init__(nam,prc)
self.wrappingcharge=wrpchrge
def get_price(self):
return self.price self.deliveryCharge self.wrappingcharge # changed to self
x1=product("yoyo",29)
print("I am buying a {} and it costs me {}".format(x1.get_name(),x1.get_price()))
m1=gift("yoyo",29)
print("I am buying a {} and it costs me {}".format(m1.get_name(),m1.get_price()))
print(x1.get_name()) # changed to x1 from product
結果:
I am buying a yoyo and it costs me 79
I am buying a yoyo and it costs me 179
yoyo
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506616.html
