類的靜態方法和類方法


一:調用區別

class Demo(object):

    @staticmethod
    def static_method():
        return "static_method"
    @classmethod
    def instance_method(self):
        return "instance_method"

    def normal_method(self):
        return "normal_method"



d = Demo()
static_result = Demo.static_method()
print(d.static_method()) # 實例調用靜態方法
print(static_result) # 類名調用靜態方法
instance_result = d.instance_method()
print(instance_result) # 實例調用類方法
print(Demo.instance_method()) # 類名調用實例方法
normal_result = d.normal_method()
print(normal_result) # 實例調用普通方法 
print(Demo.normal_method()) # 類名調用普通方法,出錯,TypeError: normal_method() missing 1 required positional argument: 'self'



# 結果
static_method
static_method
Traceback (most recent call last):
instance_method
instance_method
normal_method
  File "F:/pycharm測試功能文件夾/python知識點/類的靜態方法和實例方法.py", line 24, in <module>
    print(Demo.normal_method())
TypeError: normal_method() missing 1 required positional argument: 'self'

類中的普通方法,必須創建實例后進行調用,不能通過類名直接調用

類中的靜態方法和類方法,可以通過類名調用,也可以通過實例進行調用

二:獲取屬性的區別

靜態方法:靜態方法中不能使用實例屬性和類屬性

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
         print("靜態方法獲取類屬性",self.class_attribute)
         print("靜態方法獲取實例屬性",self.normal_attribute)
    # @classmethod
    # def instance_method(self):
    #     print("類方法獲取類屬性", self.class_attribute)
    #     print("類方法獲取實例屬性", self.normal_attribute)
    #
    # def normal_method(self):
    #     print("實例方法獲取類屬性", self.class_attribute)
    #     print("實例方法獲取實例屬性", self.normal_attribute)



d = Demo()

Demo.static_method()
d.static_method()

# Demo.instance_method()
# d.instance_method()
#
# d.normal_method()
# Demo.normal_method()

# 結果
Traceback (most recent call last):
  File "F:/pycharm測試功能文件夾/python知識點/類的靜態方法和實例方法.py", line 24, in <module>
    Demo.static_method()
TypeError: static_method() missing 1 required positional argument: 'self'

 

類方法:只能使用類屬性,不能使用實例屬性

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    # @staticmethod
    # def static_method(self):
    #      print("靜態方法獲取類屬性",self.class_attribute)
    #      print("靜態方法獲取實例屬性",self.normal_attribute)
    @classmethod
    def instance_method(self):
        print("類方法獲取類屬性", self.class_attribute)
        # AttributeError: type object 'Demo' has no attribute 'normal_attribute'
        # print("類方法獲取實例屬性", self.normal_attribute)  
    #
    # def normal_method(self):
    #     print("實例方法獲取類屬性", self.class_attribute)
    #     print("實例方法獲取實例屬性", self.normal_attribute)



d = Demo()

# Demo.static_method()
# d.static_method()

Demo.instance_method()
d.instance_method()
#
# d.normal_method()
# Demo.normal_method()


# 結果
類方法獲取類屬性 1
類方法獲取類屬性 1

 

實例方法:既可以使用實例屬性,也可以使用類屬性

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    # @staticmethod
    # def static_method(self):
    #      print("靜態方法獲取類屬性",self.class_attribute)
    #      print("靜態方法獲取實例屬性",self.normal_attribute)
    # @classmethod
    # def instance_method(self):
    #     print("類方法獲取類屬性", self.class_attribute)
        # AttributeError: type object 'Demo' has no attribute 'normal_attribute'
        # print("類方法獲取實例屬性", self.normal_attribute)
    #
    def normal_method(self):
        print("實例方法獲取類屬性", self.class_attribute)
        print("實例方法獲取實例屬性", self.normal_attribute)



d = Demo()

# Demo.static_method()
# d.static_method()

# Demo.instance_method()
# d.instance_method()
#
d.normal_method()
# Demo.normal_method()


# 結果
實例方法獲取類屬性 1
實例方法獲取實例屬性 2

 

三:獲取方法的區別

靜態方法:方法內部既不能調用實例方法也不能調用類方法

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
        print(self.instance_method())
        print(self.normal_method())
        return "static_method"
    @classmethod
    def instance_method(self):
        return "instance_method"

    def normal_method(self):
        return "normal_method"



d = Demo()

Demo.static_method()
d.static_method()

# Demo.instance_method()
# d.instance_method()
#
# d.normal_method()
# Demo.normal_method()


# 結果
Traceback (most recent call last):
  File "F:/pycharm測試功能文件夾/python知識點/類的靜態方法和實例方法.py", line 23, in <module>
    Demo.static_method()
TypeError: static_method() missing 1 required positional argument: 'self'

類方法:內部可以調用實例方法(需要參數self),也可以調用靜態方法(需要參數self)

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
        return "static_method"
    @classmethod
    def instance_method(self):
        print(self.static_method(self))
        print(self.normal_method(self))
        return "instance_method"

    def normal_method(self):
        return "normal_method"



d = Demo()

# Demo.static_method()
# d.static_method()

Demo.instance_method()
d.instance_method()
#
# d.normal_method()
#  Demo.normal_method()

# 結果
static_method
normal_method
static_method
normal_method

 

實例方法:內部可以調用類方法和靜態方法(需要傳遞self)

class Demo(object):
    class_attribute = 1

    def __init__(self):
        self.normal_attribute= 2

    @staticmethod
    def static_method(self):
        return "static_method"
    @classmethod
    def instance_method(self):
        return "instance_method"

    def normal_method(self):
        print(self.static_method(self))
        print(self.instance_method())
        return "normal_method"



d = Demo()

# Demo.static_method()
# d.static_method()

# Demo.instance_method()
# d.instance_method()
#
d.normal_method()
# Demo.normal_method()

# 結果
static_method
instance_method

 

 

#TODO


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM