什么是继承
"""
继承就是让子类拥有父类的属性和方法
子类 - 继承者
父类 - 被继承者
"""
继承的语法
"""
class 类名(父类1, 父类2,..)
pass
定义类的时候如果没有写继承关系,那么这个类默认继承python的基类: object
class 类名: == class 类名(object)
"""
# 子类继承父类的属性和方法示例
class Person:
num = 61
def __init__(self):
self.name = '小明'
self.age = 18
self.gender = '男'
def func1(self):
print(f'{self.name}今年{self.age}岁!')
@classmethod
def func2(cls):
print(f'人类的数量: {cls.num}')
@staticmethod
def func3():
print('人类破坏环境!')
class Student(Person):
pass
print(Student.num)
stu = Student()
print(stu.name, stu.age, stu.gender)
stu.func1()
Student.func2()
Student.func3()
# 3.子类中添加属性和方法
"""
1) 添加类属性、方法
直接在子类中添加新的类属性和新的方法
2) 添加对象属性
在子类中的__init__方法中添加新的对象属性,同时使用super()去调用父类的__init__
super的用法:
super(类, 对象).方法() - 调用指定类的父类的指定方法
"""
# 4.类中的方法的调用
"""
在通过类或者对象调用方法的时候,会先看当前类是否存在这个方法,如果存在就直接调用,如果不存在就看父类中有没有对应的方法,如果有
就父类中的这个方法,父类也没有就看看父类的父类...以此类推,如果找到基类都没有找到这个方法,程序才报错!
"""
class A:
x = 10
def __init__(self):
print('A: init')
self.aa = 10
self.bb = 20
def func1(self):
print('A中的对象方法')
class B(A):
y = 'abc'
def __init__(self):
# super(B, self).__init__()
super().__init__() # 调用当前类的父类的__init__方法
self.study_id = '001'
self.school = '清华大学'
def func2(self):
print('B中的对象方法')
@classmethod
def func3(cls):
print('B中的类方法')
print('=============================')
b = B()
b.func1()
b.func2()
print(B.x, B.y)
B.func3()
print(b.school, b.study_id)
print(b.aa, b.bb)
class M:
def func1(self):
print('M')
class N(M):
def func1(self):
print('N')
class X:
def func1(self):
print('X')
@classmethod
def func2(cls):
print('XX')
class Y(X):
def func1(self):
print('Y')
def __init__(self):
super().func1()
# super(Y, self).func1()
# super(N, N()).func1()
# super(X, X()).func1()
@classmethod
def test(cls):
super().func2()
print('================================')
y = Y()
Y.test()
class AA:
def __init__(self):
print('AA')
self.xx = 10
class BB(AA):
def __init__(self):
# super().__init__()
# AA().__init__()
AA.__init__(self)
# print('BB')
self.yy = 20
print('--------------------------')
b = BB()
print(b.yy)
print(b.xx)
代码示例
class Animal:
num = 100
def __init__(self):
self.gender = '雌'
self.age = 1
def func1(self):
print('动物的对象方法')
class Fly:
name = '飞行器'
def __init__(self):
self.max_height = 0
self.time = 0
self.speed = 0
def func2(self):
print('飞行器的对象方法')
class Bird(Fly, Animal):
pass
print(Bird.num) # 100
print(Bird.name) # 飞行器
b = Bird()
b.func1() # 动物的对象方法
b.func2() # 飞行器的对象方法
# print(b.age, b.gender)
print(b.max_height, b.speed, b.time) # 0 0 0
访问权限
"""
类的内容的访问权限分为三种
公开的:在类的内部和类的外部都可以使用,并且可以被继承
保护的:在类的内部可以使用,也可以被继承,但是不能类的外部使用
私有的:只能在类的内部使用,不能被继承也不能在类的外部使用
从真正意义上的访问权限上讲,python类中所有的内容都是公开的。python的私有化是假的私有化
"""
# 私有化
# 在需要私有化的属性名或者方法前加__(不能同时在名字后面也加__)
class A:
num = 100
__name = 'abc'
def __init__(self):
self.x = 100
self.__y = 200
@classmethod
def func1(cls):
print('类的内部num:', A.num)
print('类的内部的__name:', A.__name)
A.func1()
print(A.num)
# print(A.__name)
a = A()
print(a.x)
# print(a.__y)
print(a.__dict__) # {'x': 100, '_A__y': 200}
print(a._A__y)
代码示例
import smtplib
# 0.准备账号
# 1)什么邮箱 2)邮箱账号 3)邮箱密码(QQ邮箱需要先获取授权码)
# 1.登录邮箱
# 1)连接邮箱服务器
connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 2)登录邮箱账号
connect.login('648043259@qq.com', '授权码')
# 2.准备需要发送的邮件
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
# 1)创建一个空的邮件对象
email = MIMEMultipart()
# 2)设置邮件主题
email['Subject'] = Header('第一个自动发送的邮件', 'utf-8').encode()
# 3)设置邮件发送者
email['From'] = '12306 <648043259@qq.com>'
# 4)设置邮件接收者
email['To'] = '948737548@qq.com'
# 5)添加正文
content = MIMEText('hello world!', 'plain', 'utf-8')
email.attach(content)
# 3.发送邮件
connect.sendmail('726550822@qq.com', '15300022703@qq.com', email.as_string())
connect.quit()
代码示例
# 0.准备账号
# 1)什么邮箱 2)邮箱账号 3)邮箱密码(QQ邮箱先获取授权码)
# 1.登录邮箱
# 1)连接邮箱服务器
import smtplib
connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 2)登录邮箱
connect.login('hammerstone@foxmail.com', 'rabddidcagnybeij')
# 2.准备需要发送的邮件
from email.mime.multipart import MIMEMultipart # MIMEMultipart - 邮件类
from email.mime.text import MIMEText
from email.header import Header
# 1)创建一个空的邮件对象
email = MIMEMultipart()
# 2)设置邮件主题
email['Subject'] = Header('第一份自动发送的邮件', 'utf-8').encode()
# 3)设置邮件发送者
email['From'] = 'hammerstone@foxmail.com <hammerstone@foxmail.com>'
# 4)设置邮件接收者
email['To'] = '948737548@qq.com'
# 5)添加正文
with open('content.html', encoding='utf-8') as f:
content = MIMEText(f.read(), 'html', 'utf-8')
email.attach(content)
# 3.发送邮件
connect.sendmail('hammerstone@foxmail.com', '948737548@qq.com', email.as_string())
connect.quit()
代码示例
import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import os
# 1.登录邮箱
con = smtplib.SMTP_SSL('smtp.qq.com', 465) # 建立连接,con连接对象
con.login('648043259@qq.com', 'rabddidcagnybeij') # 登录邮箱
# 2.构建邮件
email = MIMEMultipart()
email['From'] = '发件人名称 <648043259@qq.com>'
email['To'] = '收件人名称 <819259227@qq.com>'
email['Subject'] = Header('发送附件', 'utf-8').encode()
# 0) 添加普通正文
content = MIMEText('啦啦啦啦啦啦!', 'plain', 'utf-8')
email.attach(content)
# 1)添加图片附件
# 打开图片
# with open('thumb-1920-859210.png', 'rb') as f:
# # 创建图片对象
# image = MIMEImage(f.read())
# # 设置图片为附件
# image['Content-Disposition'] = 'attachment; filename="aaa.png"'
# # 将图片附件添加到邮件中
# email.attach(image)
for f_name in os.listdir('file'):
with open(f'file/{f_name}', 'rb') as f:
image = MIMEImage(f.read())
image['Content-Disposition'] = 'attachment; filename="aaa.png"'
email.attach(image)
# 2)添加文本附件
with open('text/test.txt', 'rb') as f:
file = MIMEText(f.read(), 'base64', 'utf-8')
file['Content-Disposition'] = 'attachment; filename="xixixi.txt"'
email.attach(file)
# 3.发送邮件
con.sendmail('648043259@qq.com', '819259227@qq.com', email.as_string())