面向对象成员

class Provider:
    #静态字段   属于类
    country = "中国"

    def __init__(self,name):
        #普通字段  属于对象(obj)
        self.name=name

    def pro(self):
        pass
obj = Provider("河南")
print(obj.name)
print(obj.country)  #对象也你能获取到类的字段
print(Provider.country)
print(obj.pro())
# print(Provider.name) #但是类不能获取到对象否着报错 #AttributeError: type object 'Provider' has no attribute 'name
#
# '
class Foo:

    def __init__(self,name):
        self.name = name
    '''
    普通方法  必须要创建对象后才能运行,
    如果添加装饰器@staticmethod可以不用创建直接类引用
    添加装饰器后就不许要再普通方法中添加self,因为不需要对象
    '''
    @staticmethod   #静态方法,不需要创建对象
    def aoo():
        print("Aoo")
    @staticmethod
    def boo(a,b):
        print("%s----%s"%(a,b))

Foo.aoo()
Foo.boo("abc",12)

# obj = Foo("鸡毛")
# print(obj.name)
# Foo.aoo(obj)
原文地址:https://www.cnblogs.com/TKOPython/p/12361793.html