问题描述
长度:3
宽度:4
高度:5
颜色:红色
但是我目前遇到此错误:
回溯(最近通话最近): 文件“ C:/Users/chaos/OneDrive/Documents/Python/Box_Assignment.py”,第30行,在 打印(b1.get_attributes) AttributeError:“元组”对象没有属性“ get_attributes”
让我知道您的想法,下面是我的代码。
class Box():
def __init__(self,Length,Width,Height,Color):
self.length=Length
self.width=Width
self.height=Height
self.color=Color
def __str__(self):
return 'length:'+(self.length)+'width:'+(self.width)+'height:'+(self.height)+'color:'+str(self.color)
def get_attributes(self):
attributes = self.length,self.width,self.height,self.color
return attributes
b1=(3,4,5,'Red')
b2=(5,6,7,'Blue')
print(b1.get_attributes)
解决方法
(移动评论以回答问题)
创建对象时,请包括类名称:
b1 = Box(3,4,5,'Red')
在调用class方法时,请加上括号:
print(b1.get_attributes())