面向对象中的@classonlymethod 与 @classmethod的区别

Posted dream-huang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象中的@classonlymethod 与 @classmethod的区别相关的知识,希望对你有一定的参考价值。

如果要使用classonlymethod ,则需要先定义好一个classonlymethod 类。

首先我们需要明白无论是classonlymethod还是classmethod,本质都是一个类,而classonlymethod继承了classmethod。

classonlymethodz作用:只能被类调用,不能被实例对象调用。

class classonlymethod(classmethod):  # 继承classmethod
    def __get__(self, instance, cls=None): # 
        if instance is not None:
            raise AttributeError("This method is available only on the class, not on instances.")
        return super(classonlymethod, self).__get__(instance, cls)

 

his method is available only on the class, not on instances.

以上是关于面向对象中的@classonlymethod 与 @classmethod的区别的主要内容,如果未能解决你的问题,请参考以下文章