我需要创建一个矩形数据类和一个可执行程序来测试它。
这是我正在处理的完整问题:
在可执行文件的主要功能中:
提示用户输入矩形的长度和宽度。
创建一个新的Rectangle实例,其中包含由尺寸输入的尺寸 用户。
验证上述步骤,使用它们打印两个尺寸 各自的“getter”方法。
通过打印矩形区域来测试area()方法,精确到两个 小数位。
通过打印矩形周长来测试perimeter()方法, 精确到小数点后两位。
将长度更改为22.345并将宽度更改为15.789。
再次测试area()和perimeter()方法。你应该得到 结果显示在样本输出中。*
我的问题是,类文件必须与可执行文件位于同一文件夹中吗?
如果有人想提供剩下的提示,请这样做。这就是我对矩形类的看法:
class Rectangle:
def __init__(self, length, width):
self.__length = length
self.__width = width
def set_length(self, length):
self.__length = length
def set_width(self, model):
self.__width = width
def get_length(self):
return self.__length
def get_width(self):
return self.__width
def get_area(self):
return self.__getwidth() * self.getlength()
def get_perimeter(self):
return self.__getwidth() * 2 + getlength() * 2
答案 0 :(得分:1)
你的班级看起来或多或少都不错。但是,在您发布的第一张描述类设置的图片中,get_area()
和get_perimeter()
的名称应该只是area()
和perimeter()
。还有一些其他小错字,因为@PaulCornelius提到会导致错误。
您的“可执行文件”,即调用Rectangle
类方法的文件可以在另一个文件中,但不需要。例如:
class Rectangle:
#all of the above stuff
r= Rectangle(10,20)
print r.get_area()
可以正常工作,打印矩形区域。
否则,您可以创建一个包含import rectangle
的新文件来导入Rectangle
课程。这确实需要它在同一个文件夹中(或者你必须配置Python,因此它知道在哪里寻找模块)。
创建实际调用方法的脚本是验证事情正常行为的最佳方法,并可能提供有关如何修复它们的提示。例如,尝试在get_area()
上调用Rectangle
会导致错误(因为__getwidth()
未在任何地方定义,但get_width()
是。)
答案 1 :(得分:1)
通过对拼写错误进行一些更正,您可以将类定义放在主程序中:
class Rectangle:
def __init__(self, length, width): # this initializes the object with the given parameters
self.__length = length # assign length
self.__width = width # assign width
def set_length(self, length): # this method allows us pass the Rectangle object a value and set the object's length to the given value
self.__length = length # assign length
def set_width(self, width): # had 'model' here # same thing, for width
self.__width = width # assign width
def get_length(self): # this method does one thing...
return self.__length # return the Rectangle object's length
def get_width(self): # and same for width
return self.__width
def get_area(self): # this actually does something: multiplies width by height and returns that value
return self.get_width() * self.get_length() # underscores were wrong
def get_perimeter(self): # similarly, this adds double the length to double the width and returns that value
return self.get_width() * 2 + self.get_length() * 2 # underscores were wrong, missing self
def main(): # our main program, which will test our Rectangle class
length = float(input('Length? ')) # request a length and turn that string into a float
width = float(input('Width? ')) # request a width and turn that string into a float
rectangle = Rectangle(length, width) # create a new Rectangle object with the given length and width
print(rectangle.get_length(), rectangle.get_width()) # print the object's length and width, using the getters
print(round(rectangle.get_area(), 2)) # round the area to 2 places and print it
print(round(rectangle.get_perimeter(), 2)) # round the perimeter to 2 places and print it
rectangle.set_length(22.345) # calls the Rectangle object's length setter and passes it a new value, which will set the object's length to the given value
rectangle.set_width(15.789) # same for width
print(round(rectangle.get_area(), 2)) # print the area again to see the new value
print(round(rectangle.get_perimeter(), 2)) # print the perimeter again to see the new value
main() # call our main method - without this, nothing happens
注意:Getters和setter不属于Python程序。一个合适的Python Rectangle
看起来像这样:
class Rectangle:
def __init__(self, length, width):
self.length = length # no need to mask these by starting the names with _
self.width = width
@property # this lets us call this method without parentheses - it'll look like an ordinary attribute, but it's really calling a method
def area(self):
return self.length * self.width
@property
def perimeter(self):
return 2*(self.length + self.width) # the original has 2 multiplications and 1 addition, while this has 1 multiplication and 1 addition - negligible, but it's good to see this sort of thing once in a while in case you have to optimize something at some point
这将被用作:
>>> a = Rectangle(3.2,5.1)
>>> a.area
16.32
>>> a.perimeter
16.6
>>> a.length=2
>>> a.area
10.2
>>> a.perimeter
14.2
>>> a.length
2
但请注意:
>>> a.area = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
这就像rectangle.get_area() = 3
那样有意义 - 即没有。