36讲课后作业最有一题
class Rectangle:length = 5
width = 4
def setRect(self):
print("请输入矩形的长和宽...")
self.length = float(input('长:'))
self.width = float(input('宽:'))
def getRect(self):
print('这个矩形的长是:%.2f,宽是:%.2f' % (self.length, self.width))
def getArea(self):
return self.length * self.width
运行小甲鱼答案代码,显示如下,为什么跟小答案演示的不一样
>>> rect = Rectangle()
>>> rect.getRect
<bound method Rectangle.getRect of <__main__.Rectangle object at 0x1091bdf40>>
>>> rect = Rectangle()
>>> rect.setRect
<bound method Rectangle.setRect of <__main__.Rectangle object at 0x1091bdfa0>>
调用类方法记得带上括号,否则返回的是个函数体:
rect.getRect() 调用一个对象的方法需要加上括号,和调用函数的语法是一样的:
rect.getRect()
rect.setRect()
页:
[1]