Python类方法,静态方法

class Student:
    company = 'LOL'
    @classmethod
    def say_company(cls):
        print(cls.company)

Student.say_company()
与类方法界限不清晰,可以访问类,只是不通过CLS


class
Student: company = 'LOL' @staticmethod def add(a,b): print(Student.company) print("{0}+{1}={2}".format(a,b,a+b)) Student.add(1,2)

两种方法多不能访问实例变量。

posted @ 2019-04-16 21:57  文二1234  阅读(275)  评论(0编辑  收藏  举报