关于python:Rectangle OOP / classes

Rectangle OOP/classes

我需要制作一个矩形数据类和一个可执行程序来测试它。

enter image description here

这就是我要处理的全部问题:

在可执行文件的主函数中:

  • 提示用户输入矩形的长度和宽度。

  • 使用输入的维度创建新的矩形实例用户。

  • 要验证上述步骤,请使用各自的"吸气剂"方法。

  • 通过打印矩形区域来测试area()方法,精确到2小数位。

  • 通过打印矩形周长来测试周长()方法,精确到小数点后两位。

  • 将长度更改为22.345,将宽度更改为15.789。

  • 再次测试area()和periphery()方法。你应该得到结果显示在样本输出中。*

我的问题是,类文件必须和可执行文件在同一个文件夹中吗?

如果有人想提供其他方面的建议,请提供。这是我为矩形类准备的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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


只要更正一下输入错误,就可以将类定义放在主程序中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

注意:getter和setter不属于Python程序。一个合适的python Rectangle应该如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
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

这将被用作:

1
2
3
4
5
6
7
8
9
10
11
12
>>> 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

但请注意:

1
2
3
4
>>> a.area = 3
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
AttributeError: can't set attribute

这和,比如说,rectangle.get_area() = 3一样有意义,也就是说,没有意义。


你的课看起来差不多不错。但是,在您发布的描述类设置的第一张图片中,似乎get_area()get_perimeter()应该简单地命名为area()perimeter()。还有一些其他的小错误,如@paulcornelius所提到的,会导致错误。

您的"可执行文件",即调用Rectangle类方法的文件可以在另一个文件中,但不必是。例如:

1
2
3
4
class Rectangle:
    #all of the above stuff
r= Rectangle(10,20)
print r.get_area()

工作正常,打印矩形区域。

否则,您可以创建一个包含import rectangle的新文件来导入您的Rectangle类。这确实要求它在同一个文件夹中(或者必须配置python,以便它知道在哪里查找模块)。

创建实际调用方法的脚本是验证事物行为是否正常的最佳方法,并可能提供有关如何修复它们的提示。例如,试图在Rectangle上调用get_area()将导致错误(因为__getwidth()在任何地方都没有定义,但get_width()是定义的)。