-6

プログラムの実行中にエラーが発生します

Enter the length of the rectangle: 4
Enter the width of the rectangle: 2
Traceback (most recent call last):
  File "C:\Users\Shourav\Desktop\rectangle_startfile.py", line 50, in <module>
    main()
  File "C:\Users\Shourav\Desktop\rectangle_startfile.py", line 34, in main
    my_rect = Rectangle()
TypeError: __init__() missing 2 required positional arguments: 'length' and 'width'

コード:

# class definition

class Rectangle:

    def __init__(self, length, width):

        self.__length = length
        self.__width = width
        self.__area = area

    def set_length(self, length):
        self.__length = length

    def set_width(self, width):
        self.__width = width

    def get_length(self, length):
        return self.__length

    def get_width(self, width):
        return self.__width

    def get_area(self, length, width):
        area = (length * width)
        self.__area = area
        return self.__area


# main function
def main():

    length = int(input("Enter the length of the rectangle: "))
    width = int(input("Enter the width of the rectangle: "))

    my_rect = Rectangle()

    my_rect.set_length(length)
    my_rect.set_width(width)

    print('The length is',my_rect.get_length())
    print('The width is', my_rect.get_width())

    print('The area is',my_rect.get_area())
    print(my_rect)

    input('press enter to continue')


# Call the main function

main()
4

4 に答える 4

3

Rectangle初期化メソッドが 2 つの引数を必要とするクラスを定義しました。

class Rectangle:
    def __init__(self, length, width):

それでも、これらの引数を渡さずに作成しようとします:

my_rect = Rectangle()

代わりに長さと幅を渡します。

my_rect = Rectangle(length, width)

次の問題はarea定義されていないことです。おそらくそれを計算したいと思うでしょう:

class Rectangle:
    def __init__(self, length, width):
        self.__length = length
        self.__width = width
        self.get_area(length, width)

設計上の注意: 通常、Python では、そのような「プライベート」変数は使用しません。代わりに通常の属性を使用してください:

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    @property
    def area(self):
        return self.length * self.width

必要に応じて、インスタンスでこれらの属性を直接取得または設定します。

print('The length is', my_rect.length)
print('The width is', my_rect.width)
print('The area is', my_rect.area)

二重下線 ( ) で始まる属性は、__nameサブクラスが誤ってそれらを再定義するのを避けるためのものです。目的は、これらの属性が現在のクラスの内部作業に不可欠であるため、破壊されないように保護することです。名前が壊れてアクセスしにくくなったからといって、実際には非公開になるわけではなく、到達するのが難しくなるだけです。何をするにしても、たとえば Java の場合のように個人名と間違えないでください。

于 2013-07-23T14:48:53.567 に答える
0

my_rect = Rectangle()メソッドに記載されているように、それが必要lengthでありwidth、それに渡されることを宣言するときRectangle __init__

于 2013-07-23T14:49:39.783 に答える
0

Rectangle のコンストラクターには、設定しない 2 つの引数が必要です。

見る:

class Rectangle:

    def __init__(self, length, width):

    my_rect = Rectangle()

必要なもの:

    my_rect = Rectangle(length, width)

参考までに:

コンストラクターの self パラメーターは暗黙的に渡されるパラメーターであるため、渡さないでください (少なくともコードに実装することによって)。

于 2013-07-23T14:49:40.640 に答える
0

クラスで定義__init__した方法Rectangleでは、長さと幅で呼び出す必要があります。

def __init__(self, length, width):

変化する

my_rect = Rectangle()

my_rect.set_length(length)
my_rect.set_width(width)

my_rect = Rectangle(length, width)
于 2013-07-23T14:49:40.750 に答える