Python中的self用法之面向对象

class Student(object):
    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score(self):
        print("%s: %s" %(self.__name, self.__score))

    def get_name(self):
        return self.__name
    def get_score(self):
        return self.__score

    def set_score(self, score):
        self.__score = score


    def get_grade(self):
        if self.__score >= 90:
            return 'A'
        elif self.__score >= 60:
            return 'B'
        else:
            return 'C'


if __name__ == "__main__":
    student = Student("Hugh", 99)   # 输入参数
    student.print_score()           # 1
    student.set_score(88)           # 改变分数
    print(student.get_name())       # 2
    print(student.get_score())      # 3
    student.print_score()           # 4
    print(student.get_grade())      # 5

posted on 2019-10-22 10:02  星辰之衍  阅读(687)  评论(0编辑  收藏  举报

导航