class Rectangle(object):
def __init__(self, width=0.0, height=0.0):
self.width = width
self.height = height
def __setattr__(self, name, value):
if name == 'width':
self.width = value
else:
self.__dict__[name] = value
# super(Rectangle,self).__setattr__(name,value)
def get_area(self):
return self.width * self.height
width, height = float(input()), float(input())
rectangle = Rectangle(width, height)
print((rectangle.get_area()))
计算矩形面积
最新推荐文章于 2023-06-04 23:10:41 发布
这段代码定义了一个名为Rectangle的Python类,该类有两个属性:width和height,并且重写了`__setattr__`方法来控制对象属性的设置。此外,Rectangle类还包含一个计算面积的`get_area`方法。通过输入宽度和高度,创建Rectangle实例并打印其面积。
2604

被折叠的 条评论
为什么被折叠?



