|
发表于 2020-6-22 17:53:37
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class Rectangle:
- def __init__(self,width=0,height=0):
- self.width=width
- self.height=height
- def __setattr__(self,name,value):
- if name=='square':
- self.width=value
- self.height=value
- else:
- super().__setattr__(name,value)
- def getAre(self):
- return self.width*self.height
- r1=Rectangle('square',3)
- print(r1.getAre())
复制代码
为什么代码跑起来结果是打印了三次square
为什么打印三次呢,因为返回的是 self.width*self.height ,而你的传入参数是 'square' 和 3 ,也就是:
self.width = 'square'
self.height = 3
所以字符串 * 3 就是将三个相同的字符串拼接一起,所以返回了3次的 square
第二个问题:
你可能没理解 __setattr__(self,name,value) 里面的 name 参数 和 value 参数代表的是什么
name 代表的就是赋值的变量名, value 代表的是 赋值的值,简单点说就是 等号左边的是name 右边的是 value
然后当你 r1=Rectangle('square',3) 将实例化对象 r1 会自动调用 __init__ 方法
而 __init__ 方法里面有两个赋值操作:
self.width=width
self.height=height
这两个赋值操作会调用直接的 __setattr__ 先来看第一个 self.width=width 这里调用了自己的 __setattr__ 方法
name 指的就是 等号左边的 width ,而 value 是等号右边的 就是你传入的 'square' 字符串,进入 __setattr__ 方法 进行判断 name 是否等于 'square'
因为这里的 name = 'width' 不等于 'square' 所以不满足 if 条件执行 else
else 用super 调用基类的 __setattr__方法 来给 width 赋值为 'square'
同理 self.height=height 也是一样的
所以不会导致 无限递归调用 __setattr__方法
|
|