已经定义了Person类:
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender
定义Student类时,只需要把额外的属性加上,例如score:
class Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score
一定要用 super(Student, self).__init__(name, gender) 去初始化父类
几个函数:
isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str、list、dict,也可以用在我们自定义的类,它们本质上都是数据类型。
dir()显示变量的所有属性
type()获取变量的类型,返回一个type对象
多重继承:
class A(object): def __init__(self, a): print 'init A...' self.a = a class B(A): def __init__(self, a): super(B, self).__init__(a) print 'init B...' class C(A): def __init__(self, a): super(C, self).__init__(a) print 'init C...' class D(B, C): def __init__(self, a): super(D, self).__init__(a) print 'init D...'