一、定义类(class)
1、最简单的类
#标识 类名 object: python3可写可不写,python2必须写
class Class_name(object):
print('hello')
if __name__ == "__main__":
LL =Class_name()
" " "
#__init__是一个特殊方法用于在创建对象时进行初始化操作
" " "
class class_name(object):
#重写init函数,并不是新的定义,但必须写(当用某些公共变量时在此定义)
def __init__(self):
#初始化,打标记,以便于重复利用
self.a = 1000
#打上self标记表示调用的是自己是“class_name”类的不然不能引用,self是印记的作用,不代表一个参数
def A(self):
print(self.a)
print("sdf dgh htd")
if __name__ == "__main__":
Joker = class_name #相当于走了初始化__init__
Joker.A() #在类中调用使用.
print(Joker.a)
" " "
2.进度条
" " "
**#常规进度**
mport time
class ll(object):
def __init__(self):
pass
def jindutiao(self):
for i in range(1,101):
time.sleep(0.5)
print('#'*(i),'%d %%'%i)
if __name__ == "__main__":
LL = ll()
LL.jindutiao()
" " "
" " "
#20个#,下载进度还是100%
import time
class ll(object):
def __init__(self):
self.count = 1
def jindutiao(self):
for i in range(0,100):
time.sleep(0.1)
if i % 5 ==0:
res = '#'*self.count
print('%s %d%%'%(res,(i+5)))
self.count += 1
if __name__ == "__main__":
LL = ll()
LL.jindutiao()
" " "
" " "
#FLUSH刷新,传参,且在一行上出#
" " "
import time
class ll(object):
def jindutiao(self,count):
for i in range(0,100):
time.sle d: str
print('%d%%\r'%(i+1),end="",flush=True)
if i % 5 == 0:
res = '#'*count
print('%s %d%%\r'%(res,(i+5)),end)
count += 1
print()
if __name__ == "__main__":
LL = ll()
LL.jindutiao(count=1)
" " "
3.#设置私有变量
class LL(object):
def __init__(self,path):
self.path = pathself.count =0
def read(self):
with open(self.path,modelines()
for line in lines:
new_line = line.strip('\n')
for i in new_line:
if i.isupper():
self.count += 1
print('The Upper word is %d'%self.count)
if __name__ == "__main__":
path = 'users/LL/Desktop/test.txt'
ll = LL(path)
ll.read()