当前位置:网站首页>实例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电子书!↓↓↓↓↓↓
"""边栏推荐
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- Web3 security risks daunting?How should we respond?
- boot issue
- Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
- VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- jmeter跨平台运行csv等文件
- 可变字符串
- Sky map coordinate system to Gaode coordinate system WGS84 to GCJ02
- typescript58 - generic classes
猜你喜欢
随机推荐
JS 从零教你手写节流throttle
【OpenCV】-重映射
The 600MHz band is here, will it be the new golden band?
typescript53-泛型约束
js中常用的几种遍历处理数据的方法梳理
JS 保姆级贴心,从零教你手写实现一个防抖debounce方法
贴纸拼词 —— 记忆化搜索 / 状压DP
循环绕过问题
jmeter distributed stress test
工程制图复习题(带答案)
【Untitled】
Slipper —— 虚点,最短路
nodejs+express实现数据库mysql的访问,并展示数据到页面上
数组_滑动窗口 | leecode刷题笔记
Demand analysis of MES management system in electronic assembly industry
工程制图名词解释-重点知识
螺旋矩阵_数组 | leecode刷题笔记
Quickly build a website with static files
多渠道打包
this巩固训练,从两道执行题加深理解闭包与箭头函数中的this









