您的位置:首页 > 其它

静态方法

2015-10-12 17:04 260 查看
# -*- 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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: