通常情况下,如果我们要使用一个类的方法,那我们只能将一个类实体化成一个对象,进而调用对象使用方法。

式例 1

比如:

class Hello(object):
    def __init__:
        ...

def print_hello(self):
        print "Hello"

要用 print_hello() 就得:

hlo = Hello()
hlo.print_hello()
Hello

如果用了 @classmethod 就简单了。

class Hello(object):
    def __init__:
        ...

@classmethod
    def print_hello(cls):
        print "Hello"

要用的话直接:

Hello.print_hello()
Hello

注意:@classmethod 仅仅适用于单独的,与类本身的数据结构无关函数,其实用了它的函数,与使用普通函数无异,甚至不能在参数里加入 self,如果要在其中使用类的数据结构,仍然需要将类实例化一次才可以,所以要小心使用。

式例 2 

class Room:
t = 1
def __init__(self):
pass @classmethod #classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数(python默认自动会加这样一个参数),可以来调用类的属性,类的方法,实例化对象等。
def status(cls,x): #cls 是由Room自动会传递过来,这是python做的
print(cls, x, cls.t) #<class '__main__.Room'> 10 1 Room.status(10) #Room 会自动传递给cls 10会传递给x

类方法@classmethod的更多相关文章

  1. python-静态方法staticmethod、类方法classmethod、属性方法property

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def  ...

  2. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  3. Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  4. python类方法@classmethod与@staticmethod

    目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...

  5. 静态方法staticmethod和类方法classmethod

    静态方法staticmethod和类方法classmethod 一.类方法classmethod 把一个方法变成一个类中的方法,这个方法可以直接利用类来调用,不需要依托任何的对象,即不需要实例化也可以 ...

  6. Python - 静态方法@staticmethod和类方法classmethod

    传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...

  7. python静态属性@property、类方法@classmethod、静态方法@staticmethod和普通方法

    静态属性:即将类的函数通过@property属性封装,封装后实例调用该函数时,不再需要在函数后面加(),而是用类似调用数据属性的方式直接调用函数名称即可执行函数. 静态属性既可以访问类的属性,也可以访 ...

  8. 属性,类方法@classmethod

    # 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...

  9. Python面向对象 | 类方法 classmethod

      类方法:必须通过类的调用,而且此方法的意义:就是对类里面的变量或者方法进行修改添加. 例一个商店,店庆全场八折,代码怎么写呢? class Goods: __discount = 0.8 # 折扣 ...

随机推荐

  1. VBScript Scripting Techniques: File Open Dialog http://www.robvanderwoude.com/vbstech_ui_fileopen.php

    I accept cookies This website uses cookies to ensure you get the best experience on our website More ...

  2. CodeLite C/C+ IDE更新放出

    CodeLite IDE Revision 1145 for C/C++已经发布,这是一个强大的开源,跨平台的C/C++整合开发环境.目前已经在Windows XP SP3,Ubuntu 7.10 G ...

  3. NodeJs使用async让代码按顺序串行执行

    描述 由于nodejs中的函数调用都是异步执行的,而笔者在工程开发中函数A需要四五个参数,而这四五个参数值都是通过函数调用获得,因此按顺序写代码时,执行到函数A时,往往函数A需要的参数值因为参数的异步 ...

  4. PowerDesigner最基础的使用方法入门学习(一)

    1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自行学习) 我的PowerDesigner版本是16.5的,如若版本不一样,请自行参考学习即可.(打开软件即是 ...

  5. [UE4]制作缩略图

    一.创建一个专门用来做缩略图的角色CameraCharacter,不需要实体模型. 二.Auto Possess Player设置为“Player 0” 三.重力比例改成0(这样在天上的时候就不会往下 ...

  6. [UE4]扔枪

    1.把枪Detach掉:DetachFromActor 3个都选择“Keep World” 2.模拟物理 3.给一个向前的速度 4.切枪,到上一个武器,或者捡起脚底下的武器 注意Get Compone ...

  7. Processing Binary Protocols with Client-Side JavaScript

    http://blog.mgechev.com/2015/02/06/parsing-binary-protocol-data-javascript-typedarrays-blobs/ https: ...

  8. linux安装和卸载软件:sudo apt-get install(remove)

    sudo apt-get install xxx sudo apt-get remove xxx

  9. jstat 详解

    最近项目里面使用到了多线程,有时候多线程会存在挂掉的情况,趁机好好学习总结一下JVM调优的方法. jstat使用: #jstat -help|-options #jstat -<option&g ...

  10. Change default network name (ens33) to old “eth0” on Ubuntu 18.04 / Ubuntu 16.04

    Change default network name (ens33) to old “eth0” on Ubuntu 18.04 / Ubuntu 16.04 By Raj Last updated ...