私有屬性代碼結果
# -*- coding: UTF-8 -*-
class Person(object):
? ? def __init__(self, name, score):
? ? ? ? self.name = name
? ? ? ? self.__score = score
? ? def get_grade(self):
? ? ? ? if self.__score >= 85:
? ? ? ? ? ? print("A-優秀")
? ? ? ? elif self.__score >= 60:
? ? ? ? ? ? print("B-及格")
? ? ? ? else:
? ? ? ? ? ? print("C-不及格")
? ? ? ? ? ??
p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
為什么結果有none?
2019-01-10
2019-03-18
因為你的getter方法也就是get_grade這個返回值為none,外部的print p1.get_grade()把這個返回值打印出來了