我是一名有用的助手,可以为您翻译文本。
我写了这个程序来学习面向对象编程的基础知识。当我在Python Shell中从IDLE运行此程序时,它没有显示任何错误,但也没有打印任何内容...我不确定如何找出我的错误。
以下是我的代码:
class Shapes(object):
def __init__(self, width, length):
object.__init__(self)
self.setWidth(width)
self.setLength(length)
def getWidth(self):
return self.width
def setWidth(self, width):
if (width <= 0):
width = 5
else:
width = self.width
def getLength(self):
return self.length
def setLength(self, length):
if (length <= 0):
length = 10
else:
length = self.length
class Rectangle(Shapes):
def __init__(self, area, perimeter):
Shapes.__init__(self, length, width)
def getArea(length, width):
return length * width
def getPerimeter(length, width):
return (length * 2) + (width * 2)
def getStats(self):
print("Area: {}".format(self.getArea()))
print("Perimeter: {}".format(self.getPerimeter()))
print("Length: {}".format(self.getLength()))
print("Width: {}".format(self.getWidth()))
def main():
print("Rectangle a: ")
a = Rectangle(5, 7)
print("Area: {}".format(a.area))
print("Perimeter: {}".format(a.perimeter))
print( " ")
print("Rectangle b: ")
b = Rectangle()
b.width = 10
b.height = 20
print(b.getStats())
以下是Shell正在做的事情,如果您想查看:
http://imgur.com/DxyUZyY我做错了什么,如何纠正?