您的位置:首页 > 编程语言 > Python开发

python中的静态方法和类方法

2017-08-29 16:44 246 查看
#coding=utf-8
import time
import datetime

# !/usr/bin/env python
# _#_ coding:utf-8 _*_

class MyClass(object):
message = "hello world"
def show(self):
print self.message
print 'name is : %s, age is :%d' % (self.name, self.age)

@staticmethod
def printMsg():
print "print MSG"
print MyClass.message

@classmethod
def people(cls, name, age):
print "%s peope with %s is %d years old" % (cls.__name__,name,age)
return cls(name, age)

def __init__(self, name = "bai", age = 12):
print "constructor", name
self.name = name
self.age = age

MyClass.printMsg()

people = MyClass.people("jack", 20)
print people.name
print people.age
print people.message
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python