day 27 多态 接口 类方法 静态方法 hashlib 摘要算法模块

# 多态的理解:
# Python 天生自带多态
# 鸭子类型 list 和 tuple 就是一对鸭子类型 很像但是没有继承关系
#
# 而其他的类型 上传参数或者打印参数的时候 必须是指定的数据类型
# ---------
# 接口类的为了规范一个模板
# # 简单版的 必须符合继承父类的规范否则报错
# class Payment:
# def pay(self,money):
# raise NotImplementedError
# class Alipay(Payment):
# def pay(self,money):
# print('支付宝支付了%s元'%money)
# class Applepay(Payment):
# def pay(self,money):
# print('apple支付了%s元'%money)
# class Wechatpay(Payment):
# def fukuan(self,money):
# print('微信支付了%s元'%money)
# def pay(payment,money):
# return payment.pay(money)
# app = Applepay()
# pay(app,100) # 这里正常的函数上传 返回app.pay(100) 正好调用类的方法 打印出内容
# wechat = Wechatpay()
# pay(wechat,200) # 上传返回没有问题 但是返回后在在调用方法名不一样了
# ------------
# 这种继承多种方法的接口规范 只有不满足父类的全部方法就会报错
# from abc import ABCMeta,abstractmethod
# class Payment(metaclass=ABCMeta):
# @abstractmethod
# def pay(self,money):pass
# # @abstractmethod
# # def eat(self,monye):pass
# class Alipay(Payment):
# def pay(self,money):
# print('支付宝支付了%s元'%money)
# class Applepay(Payment):
# def pay(self,money):
# print('apple支付了%s元'%money)
# class Wechatpay(Payment):
# def fukuan(self,money):
# print('微信支付了%s元'%money)
# def pay(payment,money):
# return payment.pay(money)
# app = Applepay()
# pay(app,100) # 这里正常的函数上传 返回app.pay(100) 正好调用类的方法 打印出内容
# wechat = Wechatpay() #报错
# pay(wechat,200) # 上传返回没有问题 但是返回后在在调用方法名不一样了
# ------------------------
# 私有属性 伪装属性@property 把方法变成属性(前提方法名只有self一个参数,有返回值
# 在此基础上@方法名.setter 设置修改静态属性 这里可以有两个参数
# 在此基础上@方法名.deleter 设置修改静态属性 这里可以有两个参数
# 简单版
# class Goods:
# __discount = 0.8
# def __init__(self,name,price):
# self.name = name
# self.__price = price
# def price_zh(self):
# return self.__price*Goods.__discount # 注意错点 这里的静态属性 引用需要类名.静态属性名
# app = Goods('苹果',10)
# print(app.price_zh())
# --------升级版 运用@property() setter() delattr()
# 需求 原价10元 改为8元 折后6.4 ,
# class Goods:
# __discount = 0.8
# def __init__(self,name,price):
# self.name = name
# self.__price = price
# @property
# def price_z(self):
# return self.__price*Goods.__discount # 注意错点 这里的静态属性 引用需要类名.静态属性名
# @price_z.setter
# def price_z(self,new_price):
# # # # if type(new_price)is float:
# # # # return self.__price == new_price #错点不需要返回值的
# # #if type(new_price)is int: #错点 如果是后边is float的话 输入必须是小数才行
# if type(new_price) is float:
# self.__price = new_price
# @
# app = Goods('苹果',10)
# app.price_z = 6.0 #上边用float
# app.price_z = 6.0 #上边用int
# # print(app.price)
# print(app.price_z)
# -----------
# 修改属性 并删除属性
# class Goods:
# __discount = 0.8
# def __init__(self,name,price):
# self.name = name
# self.__price = price
# @property
# def name_z(self):
# return self.name # 注意错点 这里的静态属性 引用需要类名.静态属性名
# @name_z.setter
# def name_z(self,new_name):
# self.name = new_name
# @name_z.deleter
# def name_z(self, new_name):
# del self.name
# #
# app = Goods('苹果',10)
# app.name_z = '香蕉'
# # print(app.name_z) # 修改成功
# # del app.name_z
# # print(app.name_z) #删除成功
# -------------------------------------------
# 类里面 普通方法self 类方法 cls 静态方法 staticmethod 绑定方法
# 四、classmethod和staticmthod(类方法、静态方法)
# 问题:定义一个类类里面的方法并没有用到self
# 例如:
# class Goods:
# __discount = 0.8
# def change_discount(self,new_discount):
# Goods.__discount = new_discount
# apple = Goods() # 实例化
# apple.change_discount(0.5)
# -----
# 类方法 @classmethod在不需要实例化的时候不用对象self相关
# 对静态属性进行修改的
# class Goods:
# __discount = 0.8
# @classmethod
# def change_discount(cls,new_discount): #和修改一样的
# cls.__discount = new_discount
# @classmethod
# def get_discount(cls): #方法得到 返回值
# return cls.__discount
# Goods.__discount = 0.5
# print(Goods.__discount)
#
# -----------------------
# 静态方法 @staticmethod
# 在不需要
# 如果这个方法既不需要操作静态变量,也不需要使用对象相关的操作,就使用静态方法
# class A:
# def func(): # 显示错误 但是可以运行的
# print(123) # 和类 对象也没有关系的
# A.func('alex')
# # ------
# class A:
# @staticmethod #静态方法的标志
# def func(name): #静态方法
# print(123) # 和类 对象也没有关系的
# A.func('alex')
# ---------
#-------------------------------------------
# hashlib 摘要算法模块 md5 和sha 一样的 难易程度 时间 长度不一样的
# 模板
# import hashlib
# objice1 = hasattr.md5(加盐的地方)
# objice1.update(b'摘要运算的内容') #字节码
# print(objice1.hexdigest())

# 加盐的 给 我的名字 加密 加盐的地方也有中文 运用md5
# import hashlib
# # name = hashlib.md5('加密了,加密了'.encode('utf-8')) # 中文的第一种
# name = hashlib.md5(bytes('加密了,加密了',encoding = 'utf-8')) #中文的第二种
# name.update(bytes('我的名字',encoding = 'utf-8')) #摘要内容的第一种
# # name.update('我的名字'.encode('utf-8')) #摘要内容的第二种
# print(name.hexdigest()) #注意易错hexdigest() 括号
#
# # 827b9d5333d7b979fc25d6fc14928014

# import hashlib
# mdd5 = hashlib.md5(b'lishi')
# mdd5.update(b'jassin')
# print(mdd5.hexdigest())
# 中文除了字母就用 b

# 关于文件的 摘要算法 加密 userinfo文件夹名字 用户名 lishi 密码jassin
# import hashlib
# with open('userinfo','w')as f:
# wang = hashlib.md5(b'123ABC')
# wang.update(b'jassin')
# wangmd5 = wang.hexdigest()
# f.write('lishi|%s'%wangmd5)
# uesrname = input('username:')
# f = open('userinfo')
# lishi_info = f.readline().strip()
# user,passward = lishi_info.split('|')
# wang = hashlib.md5(b'123ABC')
# wang.update(bytes(pwd,encoding='utf-8'))
# if passward ==wang.hexdigest() and username ==user:
# print('登录成功')
# else:
# print('登录失败')

# import hashlib
# #存储密文密码
# f = open('userinfo2','w')
# md5 = hashlib.md5(b'3714') #加盐处理
# md5.update(b'jassin')
# md5_value = md5.hexdigest()
# f.write('lishi|%s'%md5_value)
# f.close()
# #登录读取验证
# username = input('username:')
# pwd = input('pwd! ')
# f = open('userinfo2')
# lishi_info = f.readline().strip()
# user,passwd = lishi_info.split('|') # 用户名,密码
# md5 = hashlib.md5(b'3714') #加盐处理
# md5.update(bytes(pwd,encoding='utf-8'))
# if passwd == md5.hexdigest() and username == user:
# print('登录成功')
# else:
# print('我会对你说上一世情话')

原文地址:https://www.cnblogs.com/xiaoluoboer/p/7899795.html