当前位置:网站首页>实例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电子书!↓↓↓↓↓↓
"""边栏推荐
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
- 可变字符串
- FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
- typescript58 - generic classes
- 敏捷交付的工程效能治理
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
- Quickly build a website with static files
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
- Slipper —— 虚点,最短路
猜你喜欢

Quickly build a website with static files

typescript51-泛型的基本使用

Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)

typescript53 - generic constraints

flask框架初学-06-对数据库的增删改查

typescript57 - Array generic interface

How to find the cause of Fiori Launchpad routing errors by single-step debugging

MySQL回表指的是什么

nodejs安装及环境配置
![Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.](/img/10/87c0bedd49b5dce6fbcd28ac361145.png)
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
随机推荐
nodejs切换版本使用(不需要卸载重装)
【QT小记】QT中信号和槽的基本使用
typescript52 - simplify generic function calls
Quickly build a website with static files
What warehouse management problems can WMS warehouse management system solve in the electronics industry?
splice随机添加和删除的写法
【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
Linux安装mysql最简单教程(一次成功)
静态/动态代理模式
多渠道打包
企业虚拟偶像产生了实质性的价值效益
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程
如何用C语言代码实现商品管理系统开发
可变字符串
typescript53-泛型约束
虚拟机CentOS7中无图形界面安装Oracle
Installation and configuration of nodejs+npm
typescript56 - generic interface
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
【日志框架】