|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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 getArea(self):
- return self.width*self.height
复制代码
def __init__(self,width=0,height=0):
这里的width和height为什么要赋值为0直接改成
def __init__(self,x,y):也行啊
def __init__(self,width=0,height=0):
这里的width和height为什么要赋值为0直接改成
def __init__(self,x,y):也行啊
嗯,是可以的,只不过第一个是设置了默认值,所以当类在实例化对象时候,可以不一定要传入参数
而你第二中定义构造函数的参数时候,没有设置参数的默认值,所以你类在实例化对象时候就必须传入参数的值
|
|