面向对象中的@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)
posted @ 2018-08-02 20:29  CLuke  阅读(315)  评论(0编辑  收藏  举报
Live2D