通常情况下,类中函数中定义的所有函数,,都是对象的绑定方法,除此之外,还有专门的静态方法和类方法,这两个是专门给类使用的,但是对象非要调用也是不会报错的。
对象在调用的时候会把自己传递给self,也就是绑定方法的第一个参数。
1 静态方法
这里定义spam的时候没有self,而是传入了xyz,类在使用的时候必须传入3个参数
1 2 3 4 5 6 7
| class Foo: @staticmethod # spam = staticmethod(apam) def spam(x,y,z): print(x,y,z) Foo.spam(1,2,3)
|
应用场景
首相先了解下时间模块的使用
1 2 3 4 5 6 7
| >>>import time >>> time.localtime() time.struct_time(tm_year=2017, tm_mon=4, tm_mday=22, tm_hour=20, tm_min=7, tm_se c=41, tm_wday=5, tm_yday=112, tm_isdst=0) >>> t=time.localtime() >>> print(t.tm_year) 2017
|
应用场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import time class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day @staticmethod def now(): t = time.localtime() obj = Date(t.tm_year,t.tm_mon,t.tm_mday) return obj date_now =Date.now() print(date_now.year) print(date_now.month) print(date_now.day)
|
date_now =Date.now() 对类来说就是一种实例化,
专门给类用的
增加一个新的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import time class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day @staticmethod def now(): t = time.localtime() obj = Date(t.tm_year,t.tm_mon,t.tm_mday) return obj @staticmethod def tomorrow(): t1 = time.localtime(time.time()+86400) obj = Date(t1.tm_year, t1.tm_mon, t1.tm_mday) return obj date_now =Date.now() print(date_now.year) print(date_now.month) print(date_now.day) date_tomorrow = Date.tomorrow() print(date_tomorrow.year) print(date_tomorrow.month) print(date_tomorrow.day)
|
2 类方法
类方法是专门类的绑定方法
1 2 3 4 5 6 7 8
| class Foo: def bar(self): pass @classmethod # 类的绑定方法 def test(cls,x): print(cls,x) print(Foo.bar) print(Foo.test)
|
结果:
function Foo.bar at 0x0000000002B0AB70> 类的函数
bound method Foo.test of > 类的绑定方法
类的绑定方法和对象的绑定方法是一样的,会把类本身当做第一个参数传递给类的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Foo: def bar(self): pass @classmethod def test(cls,x): print(cls,x) cls() Foo.test(124) f = Foo() print(f.test) print(Foo.test)
|
结果:
bound method Foo.test of >
bound method Foo.test of >
打印的的都是类的绑定方法,
即便是实例一个对象,f.test(123) 也是使用的类的绑定方法
获得了类的内存地址,加括号就能实例化
str
定义在类的内部,必须返回一个字符串类型
什么时候出发执行?打印这个类的对象时,会出发执行
class People:
def init(self,name,age):
self.name=name
self.age = age
def __str__(self):
return 'name:%s,age:%s' %(self.name,self.age)
p1=People(“aa”,18)
print(p1)
应用场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import time class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day @staticmethod def now(): t = time.localtime() obj = Date(t.tm_year,t.tm_mon,t.tm_mday) return obj @staticmethod def tomorrow(): t1 = time.localtime(time.time()+86400) obj = Date(t1.tm_year, t1.tm_mon, t1.tm_mday) return obj class EuropeDate(Date): pass e1 = EuropeDate.now() print(e1)
|
结果:
main.Date object at 0x0000000002330B38>
也就是说自己儿子确实别人的
解决方案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import time class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day @classmethod def now(cls): print(cls) t = time.localtime() obj = Date(t.tm_year,t.tm_mon,t.tm_mday) return obj @classmethod def tomorrow(cls): t1 = time.localtime(time.time()+86400) obj = Date(t1.tm_year, t1.tm_mon, t1.tm_mday) return obj class EuropeDate(Date): def __str__(self): return 'year:%s,month:%s,day:%s' %(self.year,self.month,self.day) e1 = EuropeDate.now()
|
通过测试,此时打印的的类是Europe
class ‘main.EuropeDate’>
终极版:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import time class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day @classmethod def now(cls): print(cls) t = time.localtime() obj = cls(t.tm_year,t.tm_mon,t.tm_mday) return obj @classmethod def tomorrow(cls): t1 = time.localtime(time.time()+86400) obj = cls(t1.tm_year, t1.tm_mon, t1.tm_mday) return obj class EuropeDate(Date): def __str__(self): return 'year:%s,month:%s,day:%s' %(self.year,self.month,self.day) e1 = EuropeDate.now() print(e1)
|
结果是:
class ‘main.EuropeDate’>
year:2017,month:4,day:23
在程序中改的是
obj = cls(t.tm_year,t.tm_mon,t.tm_mday) ,这是用子类实例化
引入cls就是谁来调用,谁就执行