Discuss / Python / 参考大神的理解,自己敲了一便

参考大神的理解,自己敲了一便

Topic source

class Student(object):

    #slots限制Student类中只能有name和age两个属性

    __slots__ = ('name','age')

#小学生类继承学生类

class Pupil('score','gender'):

    __slots__ = ('score','gender')

    pass

s1 = Student()  #实例化对象

#给s1实例动态绑定允许的属性

s1.name = 'Bob'

s1.age = 25

#给s1动态绑定被限制的属性,绑定失败

#s1.score   #绑定失败

p1 = Pupil()  #实例化p1对象

#给小学生类允许的属性=父类定义的属性+自身定义的属性

p1.name = 'lucy'

p1.age = 10

p1.score = 95

p1.gender = 'female'

#绑定限制的属性,绑定失败

#p1.grade = 'geade one   #绑定失败


  • 1

Reply