Function overloading in python

Since I learn C++, there is no problem with multiple constructors in C++ programming. 
Later, after thinking about it, I suddenly realized that there is no switch statement in python. In order to give full play to flexibility, use a dictionary instead of a dictionary. 
Also in order to keep the code concise, there is only one constructor. The longest parameter list covers other constructors. 
When variable length parameters are needed, The default value is used when the number is used, that is, only one function of the same name is enough. The python code is really concise, 
but the disadvantage is that if there are no calls during compilation, even if multiple overloaded functions are defined, no error will be reported. Only when there are fewer call parameters It will be reported when the function is overloaded 
 


class stu(object):
    print ('stu')

    #默认值构造函数
    def __init__(self, stuID,stuType='Primary'):
        self.ID=stuID
        self.Type=stuType

    def display(self):
        print(self.ID,self.Type)

#使用默认值实现构造函数的多态
stu1=stu(100)
stu2=stu(200,"midschool")
#stu.display()  #不能直接调用类的一般方法
stu1.display()
stu2.display()

 

Guess you like

Origin blog.csdn.net/sichuanpb/article/details/112692868