Py函数式编程
类的定义
|
|
其中init()方法是构造函数必须的方法,用于实例初始化,生成一个新的对象并绑定对象上的属性,第一个参数必须是self指向新的对象;其余实例方法定义中第一个参数保证是self即可,实例方法可以访问class中的私有变量(__
开头的变量);
继承超类的子类想要继承超类的方法与属性需要在初始化函数中调用super().init()方法
12345class Teacher(Person):def __init__(self,name,gender,teacherCode):super(Teacher,self).__init__(name,gender)self.teacherCode = teacherCodeprint('I am a Teacher!')Python 支持多继承,一个子类可以继承多个超类的属性和方法,如果超类之间有共同的超类,重合部分只初始化一次
123456789class FootballSkilled(Person):def __init__(self,name,gender):super(FootballSkilled,self).__init__(name,gender)print('I can play football!')class FootballSkilledTeacher(FootballSkilled,Teacher):def __init__(self,*args):super(FootballSkilledTeacher,self).__init__(*args)super(FootballSkilled,self).__init__(*args)print('I am a FootballSkilledTeacher!')
在继承多类中传递参数时报错,待解决。