首页 > 编程语言 > 详细

3.Python语法-classmethod

时间:2021-01-27 14:46:36      阅读:28      评论:0      收藏:0      [点我收藏+]

classmethod 修饰符

与self类不同,不需实例化

  • 函数不需要实例化,不需要 self 参数
  • 需填写cls 参数
    - 调用类的属性,类的方法,实例化对象等
#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class A(object):
    bar = 1
    def func1(self):  
        print (‘foo‘) 
    @classmethod
    def func2(cls):
        print (‘func2‘)
        print (cls.bar)
        cls().func1()   # 调用 foo 方法
 
A.func2()               # 不需要实例化

3.Python语法-classmethod

原文:https://www.cnblogs.com/ludada/p/14334450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!