1、实例方法是从属于实例对象的方法,定义实例方法时,第一个参数必须为 self。self 指当前的实例对象。

2、调用实例方法是,不需要也不能给 self 传值,self 由解释器自动传参。

实例

class getMin():
    # 实例方法
    def fun(self, arr, n):
        print(arr[n-1])
    # 类方法
    @classmethod
    def class_fun(cls):
        print("this is class function")
 
if __name__ == "__main__":
    arr = input().strip().split(" ")
    int_arr = []
    for item in arr:
        int_arr.append(int(item))
    n = int(input())
 
    instance = getMin()
    # 用实例调用实例方法
    instance.fun(int_arr, n)
    # 用类调用方法
    getMin.fun(instance, int_arr, n)
    # 实例调用类方法
    instance.class_fun()
    # 类调用类方法
    getMin.class_fun()
原文地址:https://www.py.cn/jishu/jichu/34379.html

相关文章:

  • 2023-03-18
  • 2021-11-21
  • 2022-12-23
  • 2021-06-01
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
猜你喜欢
  • 2023-03-09
  • 2021-07-17
  • 2021-12-28
  • 2021-12-16
  • 2021-11-22
  • 2021-07-04
  • 2022-12-23
相关资源
相似解决方案