面向对象编程基础
定义类
# 标识 类名
class class_name(object):
def __init__(self):
self.a = 1000
def A(self):
print(self.a)
print('Hello')
if __name__ == "__main__":
Joker = class_name() # () 就相当于初始化__init__
Joker.A()# 在类中调用需要使用"."
例题
# 写一个进度条 20个 下载进度还是100
import time
class Joker(object):
def __init__(self):
self.count = 1
def jindutiao(self):
for i in range(0,100):
time.sleep(0.5)
print('%d%%\r'%(i+1),end="",flush=True)
if i % 5 == 0:
res = '#'*self.count
print('%s %d%%\r'%(res,(i+5)),end="",flush=True)
self.count += 1
print()
if __name__ == "__main__":
joker = Joker()
joker.jindutiao()