|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Rectangle:
#属性
long = 20
wide = 10
#方法
def set_rect(self):
long = input('请输入长 : ')
wide = input('请输入宽 : ')
def get_rect(self):
#print('现在的长是:')
print('现在的长是:',long)
print('现在的宽是:',wide)
def get_area(self):
s = long * wide
print('面积是',s)
运行后
>>> fc = Rectangle
>>> fc.get_rect()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
fc.get_rect()
TypeError: get_rect() missing 1 required positional argument: 'self'
>>>
get_rect(self)不是有加self吗。。。为什么会报错?
本帖最后由 Twilight6 于 2020-5-23 14:47 编辑
print('现在的长是:',long)
print('现在的宽是:',wide)
s = long * wide
都忘记加self了
方法调用类属性要加 self!!! - class Rectangle:
- #属性
- long = 20
- wide = 10
- #方法
- def set_rect(self):
- self.long = int(input('请输入长 : '))
- self.wide = int(input('请输入宽 : '))
- def get_rect(self):
- #print('现在的长是:')
- print('现在的长是:',self.long)
- print('现在的宽是:',self.wide)
- def get_area(self):
- s = self.long * self.wide
- print('面积是',s)
复制代码
噢 又观察了下 你input 还没转化为 整型~
|
|