#coding:utf-8

class class_name(object): class_var = 'I am a class variable' #类变量 def __init__(self): self.instance_var = 'I am a instance varibale' #成员变量(实例变量) def instance_method(self, formal_parameter): local_var_in_function = formal_parameter #实例方法局部变量 self.local_var_also_in_function = formal_parameter #实例方法局部变量 def ordinary_function(formal_parameter): print "I am an ordinary function, I can't vist class var and intance var" print self.instance_var #报错,因此普通函数无法访问成员函数 print clacc_var#报错,因此普通函数无法访问类变量 @classmethod def class_method(cls, formal_parameter): #类方法 print 'I am class method, I can visit class var and instance var' print 'I am class method, I am modifying the instance var' cls.instance_var = formal_parameter print cls.instance_var print 'I am class method, I am modifying the class var' class_var = formal_parameter print class_var @staticmethod def static_method(formal_parameter): print 'I am static method, I am the Adopted son(干儿子) for this class!!' print "I can't modify anything in the class " #print class_var #print self.instance_var print 'Get a class instance' class_instance = class_name() print '\r' print "My name is class_instance, I can call class_method, " \ "statics_method, instance_method and instance_method, but I can't call ordinary_function" print "I can show you:" print '\r' class_instance.class_method('class method is calling!!') print '\r' class_instance.static_method('static method in calling!!') print '\r' class_instance.instance_method('instance method is calling!!') print '\r' print 'class var is calling!!' print class_instance.class_var print '\r' print 'instance var is calling!!' print class_instance.instance_var print '\r' print 'Get a class!!' print '\r' print 'My name is class_name, I can call class_method, statics_method, instance_method, instance_method, and ordinary_function' print "I can show you:" print '\r' class_name.class_method('class method is calling!!') print '\r' class_name.static_method('static method in calling!!') print '\r' #class_name.instance_method("instance method can't be calling!!") print '\r' print 'class var is calling!!' print class_name.class_var print '\r' print 'instance var is calling!!' print class_name.instance_var print 'END!!'

Result:

Get a class instance


My name is class_instance, I can call class_method, statics_method, instance_method and instance_method, but I can't call ordinary_function
I can show you:


I am class method, I can visit class var and instance var
I am class method, I am modifying the instance var
class method is calling!!
I am class method, I am modifying the class var
class method is calling!!


I am static method, I am the Adopted son(干儿子) for this class!!
I can't modify anything in the class



class var is calling!!
I am a class variable


instance var is calling!!
I am a instance varibale


Get a class!!


My name is class_name, I can call class_method, statics_method, instance_method, instance_method, and ordinary_function
I can show you:


I am class method, I can visit class var and instance var
I am class method, I am modifying the instance var
class method is calling!!
I am class method, I am modifying the class var
class method is calling!!


I am static method, I am the Adopted son(干儿子) for this class!!
I can't modify anything in the class



class var is calling!!
I am a class variable


instance var is calling!!
class method is calling!!
END!!

 

类变量:

        类定义内部定义的变量(愚见,可以认为类内部没有self开头定义的变量,可以认为是类变量)

e.g. class_name中的class_var = 'I am a class variable' #类变量

 

成员变量:

         类定义内部__init__函数内以self开头定义的变量

          

e.g. self.instance_var = 'I am a instance varibale'  #成员变量(实例变量)

 

 

静态变量:

          因为Python是动态语言,不存在完全静态的变量,这个概念查阅相关资料后认为指的就是类内部定义的类变量(欢迎指正)

类方法:

          类内部定义的以@classmethod装饰的函数是类方法,类方法的调用关系可以通过print后的表述得知。

e.g. 
    @classmethod
    def class_method(cls, formal_parameter): #类方法
        print 'I am class method, I can visit class var and instance var'

        print 'I am class method, I am modifying the instance var'
        cls.instance_var = formal_parameter
        print cls.instance_var

        print 'I am class method, I am modifying the class var'
        class_var = formal_parameter
        print class_var

 

静态方法:

          类内部定义的以@staticmethod装饰的函数,类方法的调用关系可以通过print后的表述得知。

e.g.
    @staticmethod
    def static_method(formal_parameter):
        print 'I am static method, I am the Adopted son(干儿子) for this class!!'

        print "I can't modify anything in the class "
        #print class_var
        #print self.instance_var

实例方法:

          类内部定义的没有装饰器且第一个参数为self的函数,类方法的调用关系可以通过print后的表述得知。

e.g.
 def instance_method(self,  formal_parameter):
        local_var_in_function = formal_parameter    #实例方法局部变量
        self.local_var_also_in_function = formal_parameter  #实例方法局部变量

普通函数:

          类内部定义的既没有装饰器,也没有参数self的函数,类方法的调用关系可以通过print后的表述得知。

e.g.
def ordinary_function(formal_parameter):
        print "I am an ordinary function, I can't vist class var and intance var"
        print self.instance_var #报错,因此普通函数无法访问成员函数
        print clacc_var#报错,因此普通函数无法访问类变量

[1]. http://www.cnblogs.com/kex1n/p/5979366.html

[2]. http://www.cnblogs.com/funfunny/p/5892212.html

[3]. http://blog.csdn.net/linda1000/article/details/8710161

[4]. http://blog.csdn.net/gzhouc/article/details/46785797

[5]. http://blog.csdn.net/xhw88398569/article/details/48976001

[6]. http://www.cnblogs.com/Simon-xm/p/3890942.html

 

posted on 2017-01-02 15:48  Jack-su  阅读(71510)  评论(4编辑  收藏  举报