构造函数的“重载”

class Person:
    def __init__(self,*args):  
args是一个list
 
self.name= 
"" 
  
self.gender = 
"" 
  
self.age = 
0 
  
self.mobile = 
"" 
  
self.email = 
"" 
  
self.home_adress = 
"" 
 
  
if  
len(args)== 
0: 
return 
 if  
len(args) == 
1: 
self.name=args[ 
0] 
if  
len(args) == 
2: 
self.name = args[ 
0] 
self.gender= args[ 
1] 
if  
len(args) == 
3: 
self.name = args[ 
0] 
self.gender= args[ 
1] 
self.age = args[ 
2] 
if  
len(args) == 
4: 
self.name = args[ 
0] 
self.gender= args[ 
1] 
self.age = args[ 
2] 
self.mobile = args[ 
3] 
if  
len(args) == 
5: 
self.name = args[ 
0] 
self.gender= args[ 
1] 
self.age = args[ 
2] 
self.mobile = args[ 
3] 
self.email = args[ 
4] 
if  
len(args) == 
6: 
self.name = args[ 
0] 
self.gender = args[ 
1] 
self.age = args[ 
2] 
self.mobile = args[ 
3] 
self.email = args[ 
4] 
self.home_adress=args[ 
5] 
def  
say_hello( 
self): 
print( 
"大家好" 
, 
"我是"+ 
self.name+ 
"!希望大家支持我!") 
if __name__== 
"__main__": 
#===tomas=== 
 tomas=Person( 
"tomas" 
, 
"男") 
print(tomas.gender) 
print(tomas.say_hello()) 
#解决TypeError(即重载)的问题有两种: 
 # 1、参数的默认值加上。def __init__(self,name,gender,age=0,mobile="",email="",home_adress=""): 
 #2 *args
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值