
Python静态方法的使用
静态方法的定义格式如下:
@staticmethod
def static_func():
    pass
在 Student 类中,得到最低成绩和最高成绩是比较适合用静态方法来实现的,因为这两个函数不需要操作学生对象的数据。如下面的代码所示。
>>> class Student:                       # 定义类Student
…     highest_score = 0                # 类属性
…     lowest_score = 100
…     def __init__(self):              # 初始化函数
…         self.name = ""
…     @staticmethod                    # 定义静态方法,第一个参数不是self
…     def get_highest_score():
…         return Student.highest_score
…     def set_score(self, score):      # 定义普通方法
…         if score > Student.highest_score:
…             Student.highest_score = score
…         if score < Student.lowest_score:
…             Student.lowest_score = score
…
>>> student_a = Student()
>>> student_a.set_score(98)              # 普通方法
>>> student_b = Student()
>>> student_b.set_score(90)
>>> student_c = Student()
>>> student_c.set_score(92)
>>> Student.get_highest_score()          # 静态方法
98
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试
 
                     
                


