![在这里插入图片描述](https://img-blog.csdnimg.cn/cfb7c74a789a4344bef4b929c541e51e.png)
1. 面向对象的三大特性
封装特性
继承特性
多态特性
2. 对象和类
-
什么是类和对象
-
区分类和对象
3. 实例化
- 举例如下:
# 类 Class
class Cat:
# 属性: 一般为名词,eg: name, age, gender......
name = 'name'
kind = 'kind'
# 方法: 一般情况为动词,eg: create, delete, eating, run......
def eat(self):
print('cat like eating fish......')
# 对象 Object : 对类的实例化(具体化)
Outlook = Cat()
print(type(Cat)) # <class 'type'>
print(type(Outlook)) # <class '__main__.Cat'>
print(Cat) # <class '__main__.Cat'>
print(Outlook) # <__main__.Cat object at 0x000001C009364FD0>
# Outlook是由类cat实例化出的一个对象
3. 封装特性
-
什么是封装
-
构造方法__init__
-
举例如下:
- self是什么?
self实质上是实例化的对象 - self.name:将对象和name属性封装在一起
- __init__构造方法,实例化对象时自动执行的方法
# 类 Class
class Cat:
def __init__(self, name, kind): # 形参,自动执行
"""
1. 构造方法,实例化对象时自动执行的方法
2. self是什么? self实质上是实例化的对象
3. 类方法中,python解释器会自动把对象作为参数传给self
"""
print('正在执行__init__构造方法......')
print('self:', self)
# self: <__main__.Cat object at 0x0000019568104FD0>
# self是由类cat实例化出的一个对象
# 属性: 一般为名词,eg: name, age, gender......
# 封装: self.name将对象和name属性封装/绑定
self.name = name # 通过self间接调用被封装的内容
self.kind = kind # (self.属性名)
# 方法: 一般情况为动词,eg: create, delete, eating, run......
def eat(self): # 不会自动执行
print('cat %s like eating fish......' %(self.name))
# 对象 Object : 对类的实例化(具体化)
Outlook = Cat('倩倩', '大橘')
# Outlook.name 通过对象直接调用被封装的内容 (对象.属性名)
print(Outlook.name) # 倩倩
print(Outlook.kind) # 大橘
Outlook.eat() # cat 倩倩 like eating fish......
# Outlook.eat() (对象.方法名)
练习:创建一个类People
"""
创建一个类People,拥有的属性为姓名, 性别和年龄, 拥有的方法为购物,玩游戏,学习;实例化
对象,执行相应的方法。 显示如下:
小明, 18岁, 男, 去西安赛格购物广场购物
小王, 22岁, 男, 去西安赛格购物广场购物
小红, 10岁, 女, 在西部开源学习
提示:
属性: name,age,gender
方法: shopping(), playGame(), learning()
"""
class People():
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def shopping(self):
print(f'{self.name}, {self.age}, {self.gender}, 去西安赛格购物广场购物')
def learning(self):
print(f'{self.name}, {self.age}, {self.gender}, 在西部开源学习')
P1 = People('小明', 18, '男')
P2 = People('小王', 22, '男')
P3 = People('小红', 10, '女')
P1.shopping() # 小明, 18, 男, 去西安赛格购物广场购物
P2.shopping() # 小王, 22, 男, 去西安赛格购物广场购物
P3.learning() # 小红, 10, 女, 在西部开源学习
4. 继承特性
4.1 继承的实现
# 父类
class Student:
"""父类Student"""
def __init__(self, name, age):
self.name = name
self.age = age
def learning(self):
print(f'{self.name}正在学习!')
# 子类
# MathStudent中没有任何的属性和方法,直接继承父类中的方法
class MathStudent(Student):
"""MathStudent的父类是Student"""
pass
# 实例化
# m1继承父类Student中的属性和方法
m1 = MathStudent('倩倩', 21)
print(m1.name)
print(m1.age)
m1.learning() # 不会报错,子类中没有,但是父类有该方法
# m1.choice_course # 报错,子类中没有,父类也没有该方法
4.2 重写父类方法
- 父类的方法(choice_course)
# 父类
class Student:
"""父类Student"""
def __init__(self, name, age):
self.name = name
self.age = age
def learning(self):
print(f'{self.name}正在学习!')
def choice_course(self):
print('正在选课中'.center(50, '*'))
# 子类
# MathStudent中没有任何的属性和方法,直接继承父类中的方法
class MathStudent(Student):
"""MathStudent的父类是Student"""
pass
# 重写父类方法:
# 是子类中,有一个和父类相同名字的方法,在子类中的方法
# 会覆盖掉父类中同名的方法。
# def choice_course(self):
# info = """
# 课程表
# 1. 高等数学
# 2. 线性代数
# 3. 概率论
# """
# print(info)
# 实例化
# m1继承父类Student中的属性和方法
m1 = MathStudent('倩倩', 21)
m1.choice_course()
- 重写父类方法:子类的方法(choice_course)
# 父类
class Student:
"""父类Student"""
def __init__(self, name, age):
self.name = name
self.age = age
def learning(self):
print(f'{self.name}正在学习!')
def choice_course(self):
print('正在选课中'.center(50, '*'))
# 子类
# MathStudent中没有任何的属性和方法,直接继承父类中的方法
class MathStudent(Student):
"""MathStudent的父类是Student"""
# 重写父类方法:
# 是子类中,有一个和父类相同名字的方法,在子类中的方法
# 会覆盖掉父类中同名的方法。
def choice_course(self):
info = """
课程表
1. 高等数学
2. 线性代数
3. 概率论
"""
print(info)
# 实例化
# m1继承父类Student中的属性和方法
m1 = MathStudent('倩倩', 21)
m1.choice_course()
4.3 调用父类的方法
练习:先执行父类的choice_course方法,再个性化执行自己的方法
1. 方法一:直接执行父类的方法,但是不建议
# 需求: 先执行父类的choice_course方法,再个性化执行自己的方法
# 父类
class Student:
"""父类Student"""
def __init__(self, name, age):
self.name = name
self.age = age
def learning(self):
print(f'{self.name}正在学习!')
def choice_course(self):
print('正在选课中'.center(50, '*'))
# 子类
# MathStudent中没有任何的属性和方法,直接继承父类中的方法
class MathStudent(Student):
"""MathStudent的父类是Student"""
# pass
# 重写父类方法:
# 是子类中,有一个和父类相同名字的方法,在子类中的方法
# 会覆盖掉父类中同名的方法。
def choice_course(self):
# 需求: 先执行父类的choice_course方法,再个性化执行自己的方法
# 第一种解决方法: 直接执行父类的方法,但是不建议
Student.choice_course(self)
info = """
课程表
1. 高等数学
2. 线性代数
3. 概率论
"""
print(info)
# 实例化
# m1继承父类Student中的属性和方法
m1 = MathStudent('倩倩', 21)
m1.choice_course()
2. 方法二:通过super找到父类,再执行方法
# 需求: 先执行父类的choice_course方法,再个性化执行自己的方法
# 父类
class Student:
"""父类Student"""
def __init__(self, name, age):
self.name = name
self.age = age
def learning(self):
print(f'{self.name}正在学习!')
def choice_course(self):
print('正在选课中'.center(50, '*'))
# 子类
# MathStudent中没有任何的属性和方法,直接继承父类中的方法
class MathStudent(Student):
"""MathStudent的父类是Student"""
# pass
# 重写父类方法:
# 是子类中,有一个和父类相同名字的方法,在子类中的方法
# 会覆盖掉父类中同名的方法。
def choice_course(self):
# 需求: 先执行父类的choice_course方法,再个性化执行自己的方法
# 第一种解决方法: 直接执行父类的方法,但是不建议
# Student.choice_course(self)
# 第二种解决方法: 通过super找到父类,再执行方法(建议且在生产环境代码的常用方式)
super(MathStudent, self).choice_course()
info = """
课程表
1. 高等数学
2. 线性代数
3. 概率论
"""
print(info)
# 实例化
# m1继承父类Student中的属性和方法
m1 = MathStudent('倩倩', 21)
m1.choice_course()