当前位置:网站首页>实例041:类的方法与变量
实例041:类的方法与变量
2022-08-04 01:35:00 【懒笑翻】
题目:模仿静态变量的用法。
程序分析:构造类,了解类的方法与变量。
代码:
def dummy():
i = 0
print(i)
i += 1
class cls:
i = 0
def dummy(self):
print(self.i)
self.i += 1
a = cls()
for i in range(50):
dummy() # 建议这两个方法分别调用看运行效果
a.dummy()
效果:
我们发现dummy()函数的调用输出结果全部是0

我们又发现cls()类中的dummy()方法中i+=1是有效的

"""
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群,扫码下方官方推荐的群即可进入哦
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!↓↓↓↓↓↓
"""边栏推荐
猜你喜欢

typescript57-数组泛型接口

持续投入商品研发,叮咚买菜赢在了供应链投入上

企业虚拟偶像产生了实质性的价值效益

typescript50 - type specification between cross types and interfaces

typescript48 - type compatibility between functions

一个注解替换synchronized关键字:分布式场景下实现方法加锁

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

typescript58 - generic classes

GraphQL背后处理及执行过程是什么

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
随机推荐
企业虚拟偶像产生了实质性的价值效益
DDTL:远距离的域迁移学习
How to find the cause of Fiori Launchpad routing errors by single-step debugging
- heavy OpenCV 】 【 mapping
Promise solves blocking synchronization and turns asynchronous into synchronous
IDEA02:配置SQL Server2019数据库
OpenCV如何实现Sobel边缘检测
循环绕过问题
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
nodejs 安装多版本 版本切换
Tanabata festival coming, VR panoramic look god assists for you
jmeter分布式压测
2022年上半年各大厂Android面试题整理及答案解析(持续更新中......)
MongoDB数据接入实践
多渠道打包
typescript48 - type compatibility between functions
C语言:学生管理系统(链表版)
Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"