溫馨提示×

python類函數(shù)調(diào)用的方法是什么

小億
96
2024-06-04 10:10:21
欄目: 編程語言

在 Python 中調(diào)用類函數(shù)有兩種方法:

  1. 實例化類并調(diào)用實例方法:
class MyClass:
    def my_function(self):
        print("Hello, world!")

my_instance = MyClass()
my_instance.my_function()
  1. 直接調(diào)用類方法(靜態(tài)方法):
class MyClass:
    @staticmethod
    def my_function():
        print("Hello, world!")

MyClass.my_function()

0