首页 > 编程语言 >Python如何使用类里面的静态方法?

Python如何使用类里面的静态方法?

时间:2022-12-05 16:45:06浏览次数:43  
标签:里面 get Python res self host 静态方法

需求:

  • Python如何使用类里面的静态方法?

解决:

  • @staticmethod # 静态方法,是挂载在类下面的方法

  • 示例:
  • class obj:
        def __init__(self, host):
            self.host = host  # 属性
            self.n1 = self.get()
    
        @staticmethod   # 静态方法 就是挂载在类下面的方法,不依赖类
        def get():
            x = 10
            y = 20
            z = 999
            return x + y + z
    
    res = obj(888)
    print(res.n1)
    print(res.get())

     

  •  

     

标签:里面,get,Python,res,self,host,静态方法
From: https://www.cnblogs.com/liqi175/p/16952716.html

相关文章