【思维导图】python 的修饰符 静态方法@staticmethod 网络资料整合(很详细很直观的版本!嘿嘿)

【点开看大图】

在这里插入图片描述

这是讲的最明白的了
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Python进阶-----静态方法(@staticmethod)

代码2
o
代码3
oclass TestFuc(object):
 def instance_fuc(self, x):
 print(‘instance_fuc(%s,%s)’ % (self, x))
 @classmethod
 def class_fuc(cls,x):
o print(‘class_fuc(%s,%s)’ % (cls,x))
 @staticmethod
 def static_fuc(x):
o print(‘static_fuc(%s)’ % x)
otest_fuc = TestFuc()
o# 实例方法
test_fuc.instance_fuc(1)
o# 类方法
test_fuc.class_fuc(1)
TestFuc.class_fuc(1)
o# 静态方法
test_fuc.static_fuc(1)
TestFuc.static_fuc(1)
常用代码
o# staticmethod实现
oclass Date:
 def init(self,year,month,day):
 self.year=year
 self.month=month
 self.day=day
 @staticmethod
 def now():
#用Date.now()的形式去产生实例,该实例用的是当前时间
o t=time.localtime() #获取结构化的时间格式
o return Date(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
 @staticmethod
 def tomorrow():
#用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
o t=time.localtime(time.time()+86400)
o return Date(t.tm_year,t.tm_mon,t.tm_mday)
oa=Date(‘1987’,11,27) #自己定义时间
ob=Date.now() #采用当前时间
oc=Date.tomorrow() #采用明天的时间
oprint(a.year,a.month,a.day)
oprint(b.year,b.month,b.day)
oprint(c.year,c.month,c.day)
参考文献
ohttps://yuerblog.cc/2018/10/29/why-use-staticmethod/
ohttps://www.cnblogs.com/flypig258/p/11428500.html
ohttps://www.cnblogs.com/Meanwey/p/9788713.html
定义
o@staticmethod 静态方法只是名义上归属类管理,但是不能使用类变量和实例变量,是类的工具包放在函数前(该函数不传入self或者cls),所以不能访问类属性和实例属性
Clipped from:
ohttps://www.cnblogs.com/Meanwey/p/9788713.html
代码1
o1 class cal:
 2 cal_name = ‘计算器’
 3 def init(self,x,y):
 4 self.x = x
 5 self.y = y
 7 @property
#在cal_add函数前加上@property,使得该函数可直接调用,封装起来
 8 def cal_add(self):
 9 return self.x + self.y
11 @classmethod
#在cal_info函数前加上@classmethod,则该函数变为类方法,
o该函数只能访问到类的数据属性,不能获取实例的数据属性
12 def cal_info(cls):
#python自动传入位置参数cls就是类本身
13 print(‘这是一个%s’%cls.cal_name)
o#cls.cal_name调用类自己的数据属性
15 @staticmethod
#静态方法 类或实例均可调用
16 def cal_test(a,b,c):
#改静态方法函数里不传入self 或 cls
17 print(a,b,c)
o18 c1 = cal(10,11)
o19 cal.cal_test(1,2,3)
#>>> 1 2 3
o20 c1.cal_test(1,2,3)
#>>> 1 2 3
继承类中的区别
o含义
子类的实例继承了父类的static_method静态方法,调用该方法,还是调用的父类的方法和类属性。
子类的实例继承了父类的class_method类方法,调用该方法,调用的是子类的方法和子类的类属性。
o代码
class Foo(object):
 X = 1
 Y = 2
 @staticmethod
o def averag(mixes):
 return sum(mixes) / 1
 @staticmethod
o def static_method():
 return Foo.averag(Foo.X, Foo.Y)
 @classmethod
o def class_method(cls):
 return cls.averag(cls.X, cls.Y)
class Son(Foo):
 X = 3
 Y = 5
 @staticmethod
o def averag(mixes):
 return sum(mixes) / 2
p = Son()
print(p.static_method())
print(p.class_method())
staticmethod写和不写有什么区别?
o网上一般都说类静态方法要@staticmethod,而我实际试验不写也没啥问题的样子
# -
- coding: utf-8 -
- class haha: def x(): print(‘x’)haha.x()
等价
就是往对象上赋值一个属性,保存一个函数。
o# -- coding: utf-8 -- class haha: pass def x(): print(‘x’)haha.x = xhaha.x()
o那还要staticmethod干嘛?看下面的报错
# -- coding: utf-8 -- class haha: def x(): print(‘x’) obj = haha()obj.x()
多了一个实例化的过程
Traceback (most recent call last):
 File “/Users/liangdong/Documents/gitlab/try/main.py”, line 9, in
 obj.x()
TypeError:
x() takes 0 positional arguments but 1 was given
o通过类对象调用静态方法,python会自动加一个self到第一参数,而我们希望x()函数是静态方法,不需要self。
这时候就需要@staticmethod来影响这个行为了:
# -- coding: utf-8 --

class haha:
 @staticmethod
 def x():
 print(‘x’)


obj = haha()
obj.x()
haha.x()

o加上@staticmethod的haha类,无论是类还是对象都可以调用到x()方法,终于符合了类静态方法的正常行为表现。

常用代码

参考文献
https://yuerblog.cc/2018/10/29/why-use-staticmethod/
https://www.cnblogs.com/flypig258/p/11428500.html
https://www.cnblogs.com/Meanwey/p/9788713.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

认知计算_茂森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值