Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当类的_init__需要参数时,构造不带参数的对象(矩形)_Python_Python 2.7 - Fatal编程技术网

Python 当类的_init__需要参数时,构造不带参数的对象(矩形)

Python 当类的_init__需要参数时,构造不带参数的对象(矩形),python,python-2.7,Python,Python 2.7,我正在做一项作业,需要一个矩形类来计算给定的面积和周长。我们已经得到main()函数,必须围绕它构建。它似乎一直在运行,直到它到达显示它的b=Rectangle() 只需要3个参数 这是我的密码: class Shape(object): def __init__(self): pass def area(): pass def perimeter(): pass class Rectangle(Shape):

我正在做一项作业,需要一个
矩形
类来计算给定的面积和周长。我们已经得到main()函数,必须围绕它构建。它似乎一直在运行,直到它到达显示它的
b=Rectangle()

只需要3个参数

这是我的密码:

class Shape(object):

    def __init__(self):
        pass
    def area():
        pass
    def perimeter():
        pass

class Rectangle(Shape):

    def __init__(self, width, height):
        Shape.__init__(self)
        self.width = width
        self.height = height

    def area(self):
        area = self.height * self.width
        return area

    def perimeter(self):
        perimeter = 2*(self.width+self.height)
        return perimeter

    def getStats():
        print "Width:      %d" % b.width
        print "Height:     %d" % b.height
        print "Area:       %d" % b.area
        print "Perimeter:  %d" % b.perimeter

def main():

    print "Rectangle a:"
    a = Rectangle(5, 7)
    print "area:      %d" % a.area()
    print "perimeter: %d" % a.perimeter()

    print ""
    print "Rectangle b:"
    b = Rectangle()  
    b.width = 10
    b.height = 20
    print b.getStats()

main()

如何在不更改主函数的情况下使第二个矩形工作?

阅读python对“构造函数”的支持。。。差不多

   def __init__(self, width = 0, height = 0) 

好吧,这至少让它运行起来了。我正在获取矩形A的输出,但矩形B给了我以下信息:@user3550590您是否尝试搜索错误以查找任何信息?谢谢你的帮助,实际上它现在应该如何工作=)