简体   繁体   English

Python帮助:OOP矩形

[英]Python help: OOP Rectangles

Please help with completing this. 请帮助完成此操作。 I am stuck and cant seem to get it. 我被困住了,似乎无法理解。 Below this is what i was given and below the code is what the output needs to be. 在此之下是给我的,在代码下方是需要的输出。

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())

Expected Output 预期产量

When the Rectangle class has been properly created, the output should look like the following: 正确创建Rectangle类后,输出应如下所示:

Rectangle a:
area:      35
perimeter: 24
Rectangle b:
width:     10
height:    20
area:      200
perimeter: 60

this is what i have done from what i know. 这是我根据我所知道的所做的。 I missed the lecture for a family emergency and dont know how to finish it. 我错过了家庭紧急情况讲座,也不知道如何完成。

class Rectangle: 
    def __init__ (self, H=0, W=0): 
        self.width = W 
        self.height = H 

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

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.area) 
    #print (b.getStats()) 

main ()

First and foremost, you should roll a newspaper and whack the teacher on the nose for proposing the use of function named main(). 首先,您应该滚动报纸,向老师猛打,以提议使用名为main()的函数。 Python already comes with __main__ which you are welcome to use. Python已经附带了__main__ ,欢迎您使用。

As for your ongoing issue, I took a liberty of reworking your code a bit, and you should be able to follow its lead on the road to success. 至于您遇到的问题,我自由地对代码进行了一些重新设计,您应该能够在成功的道路上遵循它的领导。

class Rectangle: 
    def __init__ (self, H=0, W=0): 
        self.width = W 
        self.height = H 

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

if __name__ == "__main__": 
    a = Rectangle (5, 7) 
    print ("Rectangle a.w: %s a.h: %s" % (a.width, a.height)) 
    b = Rectangle() 
    b.width = 10 
    b.height = 20 
    print ("Rectangle b.w: %s b.h: %s" % (b.width, b.height)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM