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
本文版权归作者和博客园共有,欢迎转载,转载请注明出处和链接来源。