zoukankan      html  css  js  c++  java
  • 静态方法

    # -*- coding: utf-8 -*-
    #python 27
    #xiaodeng
    #http://blog.chinaunix.net/uid-22521242-id-4081674.html
    
    '''
    #类的方法
        共有方法
        私有方法
        类方法
        静态方法
    
    静态方法是属于类的,一般方法是属于对象的,一般方法通过对象调用,静态方法通过类调用,也可以通过实例化(对象)来调用
    有人建议尽量少用静态方法
    静态方法无需传入self参数,通过@staticmethod来修饰
    '''
    
    class people:
        country = 'china'
        
        @staticmethod
        #静态方法
        def getCountry():
            return people.country#类名.属性名
            
    if __name__=='__main__':
        print people.getCountry()
    
    
        
    print '#############################################'
    class MyClass():
        #请注意该例子是没有__init__构造函数的,但是达到了一样的效果
        name = "xiaodeng"
        
        def fun1(self):
            print 'name is:',self.name
            print "我是公有方法"
            
            #调用私有方法,__fun2
            self.__fun2()
            
                
        def __fun2(self):
            print "i am private method"
                
        @classmethod
        def fun3(self):
            print "我是类方法"
                
        @staticmethod
        def fun4():
            print "我是静态方法"
    
    if __name__=='__main__':
        zhang=MyClass()
        zhang.fun1()
        print '**'*15
        zhang.fun3()
        #调用静态方法
        zhang.fun4()
        MyClass.fun4()   
  • 相关阅读:
    索引压缩
    拼写校正
    词典(词汇表)
    Text Relatives II
    Text Relatives
    CoreText
    Quartz2D Text
    PDF Document Creation, Viewing
    Core Graphics Layer Drawing
    Bitmap Images and Image Masks
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/4872192.html
Copyright © 2011-2022 走看看