zoukankan      html  css  js  c++  java
  • 在python中使用静态方法staticmethod

    静态方法:

    静态方法是类中的函数,不需要实例。静态方法主要是用来存放逻辑性的代码,主要是一些逻辑属于类,但是和类本身没有交互,即在静态方法中,不会涉及到类中的方法和属性的操作。可以理解为将静态方法存在此类的名称空间中。事实上,在python引入静态方法之前,通常是在全局名称空间中创建函数。

    类方法:

    类方法是将类本身作为对象进行操作的方法。他和静态方法的区别在于:不管这个方式是从实例调用还是从类调用,它都用第一个参数把类传递过来

    【Reference】

    https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner/12179325#12179325

    @classmethod means: when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods). This means you can use the class and its properties inside that method rather than a particular instance.

    @classmethod的意思是:当这个方法被调用时,我们将类作为第一个参数传递,而不是该类的实例(正如我们通常使用方法所做的那样)。这意味着您可以在该方法中使用类及其属性,而不是特定的实例。

    @staticmethod means: when this method is called, we don't pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).

    @staticmethod的意思是:当这个方法被调用时,我们不会向它传递一个类的实例(就像我们通常使用方法那样)。这意味着您可以将一个函数放入一个类中,但是您不能访问该类的实例(当您的方法不使用实例时这是有用的)。

  • 相关阅读:
    css布局
    常用正则表达式
    读书笔记Hadoop实战1
    C++ IDE for Linux
    由计算机的锁是怎么实现的
    pthread
    转:C++反汇编揭秘2 – VC编译器的运行时错误检查(RTC)
    PyDev Python 开发的IDE
    转:C++反汇编揭秘1 一个简单的C++程序反汇编解析
    如何查找命令对应的软件包
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/8608029.html
Copyright © 2011-2022 走看看