python中的静态方法和类方法

来源:互联网 发布:js是什么文件格式 编辑:程序博客网 时间:2024/05/17 22:10
#coding=utf-8import timeimport 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 = ageMyClass.printMsg()people = MyClass.people("jack", 20)print people.nameprint people.ageprint people.message
原创粉丝点击