总结
一. 继承
1. 什么是继承
继承就是让子类直接拥有父类的属性和方法
子类 - 继承者
父类 - 被继承者
2. 继承的语法
"""
class 类名(父类1, 父类2,...):
pass
定义类的时候如果没有写继承关系,那么这个类默认继承python的基类: object
class 类名: == class 类名(object):
"""
3. 子类中添加属性和方法
"""
1) 添加类属性、方法
直接在子类中添加新的类属性和新的方法
2) 添加对象属性
在子类中的__init__方法中添加新的对象属性,同时使用super()去调用父类的__init__
super的用法:
super(类, 对象).方法() - 调用指定类的父类的指定方法
"""
4. 类中的方法的调用
在通过类或者对象调用方法的时候,会先看当前类是否存在这个方法,如果存在就直接调用,如果不存在就看父类中有没有对应的方法,如果有就父类中的这个方法,父类也没有就看看父类的父类...以此类推,如果找到基类都没有找到这个方法,程序才报错!
二. 多继承
对象属性只能继承第一个类的, 类属性和方法都能继承
三. 私有化
1. 访问权限
类的内容的访问权限分为三种
公开的:在类的内部和类的外部都可以使用,并且可以被继承
保护的:在类的内部可以使用,也可以被继承,但是不能类的外部使用
私有的:只能在类的内部使用,不能被继承也不能在类的外部使用
从真正意义的访问权限上讲,python类中所有的内容都是公开的。python的私有化是假的私有化
2. 私有化
在需要私有化的属性名或者方法名前加(不能同时在名字的最后也加)
四. 发送邮件的基本流程
准备需要发送邮件的邮箱账号
如果是163邮箱只需要准备账号和密码;如果是QQ邮箱,需要准备账号和授权码(密码不好用),可以去QQ邮箱官网的帮助中心去查看授权码的获取方式。
发送邮件的基本步骤
-
登录邮箱
import smtplib 1. 连接邮箱服务器 连接对象 = smtplip.SMTP_SSL(服务器地址, 邮箱服务端口) - 服务器地址:smtp.163.com(163邮箱)、smtp.qq.com(qq邮箱) - 邮箱服务端口:465或者25 2. 登录邮箱 连接对象.login(邮箱账号, 密码) - 密码:如果是163邮箱直接使用邮箱的登录密码,如果是qq邮箱就使用授权码
准备数据
-
数据指的需要发送的内容。邮件内容的构建需要涉及到另外一个库email,它可以用来构建邮件主题以及各种形式的邮件内容(包括文字内容、图片内容、html内容、附件)等,这儿先简单说一个邮件主题和文本内容,其他形式的内容在后面邮件内容部分详细讲解。
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header 1. 创建邮件对象 邮件对象 = MIMEMultipart() 2. 设置邮件主题 主题对象 = Header(邮件标题, 编码方式).encode() 邮件对象['Subject'] = 主题对象 3.设置邮件发送者 邮件对象['From'] = '用户名 <用户名>' 4.设置邮件接受者 邮件对象['To'] = '收件人1;收件人2;收件人3...' 5. 添加文字内容 文字内容对象 = MIMEText(内容, 类型, 编码方式) - 内容:就是文字字符串 - 类型:plain(简单的文字内容)、html(超文本) 邮件对象.attach(文字对象)
-
发送邮件
连接对象.sendmail(发件人, 收件人, 邮件对象.as_string()) 连接对象.quit()
示例如下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n89" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"""author=余婷"""
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
1. 连接邮箱服务器
con = smtplib.SMTP_SSL('smtp.163.com', 465)
2. 登录邮箱
con.login('XXXX@163.com', 'XXXXX')
2. 准备数据
创建邮件对象
msg = MIMEMultipart()
设置邮件主题
subject = Header('找回密码', 'utf-8').encode()
msg['Subject'] = subject
设置邮件发送者
msg['From'] = 'XXXX@163.com XXXX@163.com'
设置邮件接受者
msg['To'] = '726550822@qq.com'
添加文字内容
text = MIMEText('忘记密码需要找回密码', 'plain', 'utf-8')
msg.attach(text)
3.发送邮件
con.sendmail('xxxx@163.com', '726550822@qq.com', msg.as_string())
con.quit()</pre>
作业
class Landlords:
def __init__(self, identity='peasant'):
self.card = []
self.identity = identity
@staticmethod
def create_card():
cards = []
for x in ['♠', '♥', '♣', '♦']:
for y in ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
cards.append(f'{x}{y}')
cards.extend(['大王', '小王'])
return cards
def landlord(self, other1, other2):
from random import choice
choice((self, other1, other2)).identity = 'landlord1'
def deal(self, other1, other2):
from random import shuffle
cards = Landlords.create_card()
shuffle(cards)
landlord_card = []
count = 2
for x in cards:
count += 1
if count % 3 == 0:
self.card.append(x)
elif count % 3 == 1:
other1.card.append(x)
elif count % 3 == 2:
other2.card.append(x)
if count >= 53:
landlord_card.append(x)
if self.identity == 'landlord1':
self.card.extend(landlord_card)
elif other1.identity == 'landlord1':
other1.card.extend(landlord_card)
else:
other2.card.extend(landlord_card)
p1 = Landlords()
p2 = Landlords()
p3 = Landlords()
p1.landlord(p2, p3)
p1.deal(p2, p3)