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