封装:

    封装前先·

    1. class student(object):
    2. def __init__(self,name,score):
    3. self.name=name
    4. self.score=score
    5. std=student('sisi',9)
    6. def info(std):
    7. print('student:%s,score:%s'%(std.name,std.score))
    8. info(std)

    封装后

    1. class student1(object):
    2. def __init__(self,name,score):
    3. self.name=name
    4. self.score=score
    5. def info(self):
    6. print('student:%s,score:%s'%(self.name,self.score))
    7. st=student1('xixi',10)
    8. info(st)

    效果:
    图片.png

    数据和逻辑封装了,直接调用,不用知道内部细节。