Advanced Python syntax - object instance object properties - and instance class, class methods static methods (4.6.1)

@

1. Description

python attributes: type attribute, instance properties
methods: class methods, instance methods, static methods
want to modify class properties, class methods only, because only class methods to cls (the class) incoming data inside
the static method is the ordinary method for convenience only
example of the method, can not directly call the class to invoke the object name may be self =
DETAILED below

2. Code

class Provice(object):
    #类属性
    country = "china"

    def __init__(self,name):
        #实例属性
        self.name = name

    def self_control(self):
        print("我是实例方法,我必须有self",self.name)


        

    @staticmethod
    def static_method():
        #由类调用,无默认参数
        print("我是static_method")


    @classmethod
    def class_method(cls):

        print("我是classmethod++",cls.country)
        cls.country = "china___"



sichuan  = Provice("sichuan")
print(sichuan.name,sichuan.country,Provice.country)
Provice.class_method()
Provice.static_method()
Provice.self_control(sichuan)
sichuan.static_method()

print(sichuan.name,sichuan.country,Provice.country)


About the Author

Personal blog site
personal GitHub address
individual public number:
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/simon-idea/p/11411860.html