类的定义
class ClassName
__init__(self)构造函数:初始化对象的各属性
self代表类的实例
面向对象的特点
封装:
将数据及相关操作打包在一起
支持代码复用
继承:
子类借用父类的行为
避免重复操作,提升代码复用程度
定义class ClassName(SuperClassName)
多态
在不同情况下用一个函数名启用不同方法
灵活性
# -*- coding:utf-8
class FileTool:
def __init__(self,pathFile):
self.pathFile = pathFile
def write_to_file(self,line):
f = open (self.pathFile,'a')
f.write(line)
f.close()
def read_from_data(self):
f = open(self.pathFile,'r')
lines = f.readlines()
f.close()
return lines
class PasswordTool:
def __init__(self, password):
self.password = password
self.password_level = 0
def isNumberLoop(self):
for c in self.password:
if c.isnumeric():
return True
return False
def isPhal(self):
for c in self.password:
if c.isalpha():
return True
return False
def process_Password(self):
#规则一
if len(self.password) >= 8:
self.password_level += 1
else:
print("密码长度要求至少8位")
#规则二
if self.isNumberLoop():
self.password_level+=1
else:
print("密码要求包含数字!")
if self.isPhal():
self.password_level += 1
else:
print("密码要求包含字母!")
def main():
try_times = 5
pathFile = 'password_6.0.txt'
fileTool = FileTool(pathFile)
while try_times >0:
password = input("请输入密码")
password_tool = PasswordTool(password)
password_tool.process_Password()
line = '密码:{},强度:{}\n'.format(password,password_tool.password_level)
fileTool.write_to_file(line)
if password_tool.password_level == 3:
print("密码设置的不错o")
break
else:
try_times -= 1
print("密码很垃圾哦")
lines = fileTool.read_from_data()
for line in lines:
print(line)
if(__name__ == '__main__'):
main()