一、面向对象三大特征
面向对象三大特征是:
- 封装 (Encapsulation)
- 继承 (Inheritance)
- 多态 (Polymorphism)
1. 封装
封装就是隐藏内部实现的细节,只保留功能接口
需求
定义一个洗衣机类,其中包含了打开/关闭洗衣机门、设置洗衣模式、设置马达转速、开始洗衣服等方法。
在初始化时,需要传入品牌
brand
和容量capacity
两个参数。洗衣机门的状态is_closed
、洗衣模式__mode
和马达转速motor_speed
都有默认值。调用
wash()
方法时,会根据门的状态和模式来执行相应的操作,最终完成洗衣任务。
步骤:
- 定义
WashMachine
类,初始化时传入品牌和容量两个参数,并设置默认值。- 定义打开/关闭洗衣机门的方法,通过修改
is_closed
属性来实现。- 定义设置洗衣模式的方法,通过修改
__mode
属性来实现。- 定义设置马达转速的私有方法,通过修改
motor_speed
属性来实现。- 定义开始洗衣服的方法,根据门的状态和模式来执行相应的操作,最终完成洗衣任务。
- 实例化
WashMachine
类,传入品牌和容量两个参数,得到一个洗衣机对象。- 调用打开/关闭洗衣机门的方法,模拟打开/关闭洗衣机门的操作。
- 调用设置洗衣模式的方法,传入一个参数,设置洗衣模式。
- 调用开始洗衣服的方法,根据门的状态和模式来执行相应的操作,最终完成洗衣任务。
实现
class WashMachine:
def __init__(self, brand, capacity):
"""
初始化
:param brand: 品牌
:param capacity: 容量
"""
self.brand = brand
self.capacity = capacity
# 是否关闭
self.is_closed = False
# 模式 0:未设定模式 1:轻揉模式 2:狂揉模式
self.__mode = 0
# 马达转速
self.motor_speed = 0
def open_door(self):
self.is_closed = False
print('打开洗衣机门')
def close_door(self):
self.is_closed = True
print('关闭洗衣机门')
def set_mode(self, new_mode):
"""
调节模式
:param new_mode:
:return:
"""
if new_mode not in [1, 2]:
print('设置模式错误')
else:
self.__mode = new_mode
def __set_motor_speed(self, speed):
"""
设置马达的转速
:param speed: 1000: 轻揉模式 2000:狂揉模式
:return:
"""
self.motor_speed = speed
def wash(self):
if not self.is_closed:
# 洗衣机门是否关闭 ,没有关闭 提示
print('请关闭洗衣机门,哔哔哔哔...')
return
elif self.__mode == 0:
print('请设置模式')
return
# 执行下面的操作
print('放水...')
print('放满了...')
if self.__mode == 1:
print('轻揉模式, 洗内衣')
# 调节马达转速
self.__set_motor_speed(1000)
print('马达转速:{}'.format(self.motor_speed))
print('开始洗...')
elif self.__mode == 2:
print('狂揉模式, 洗外套')
# 调节马达抓霉素
self.__set_motor_speed(2000)
print('马达转速:{}'.format(self.motor_speed))
print('开始洗...')
print('洗完了')
machine = WashMachine('海尔', 10)
machine.open_door()
machine.close_door()
machine.set_mode(2)
machine.wash()
封装的范围:
- 封装属性
- 封装成方法/函数
- 封装成类
- 封装模块和包
2. 继承
继承指的是一个对象直接使用另一个对象的属性或方法
继承的格式:
class 子类名(父类名):
"""------------------ 定义Person类 ------------------"""
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print('hello from ', self.name)
"""------------------ 定义Student类继承Person ------------------"""
class Student(Person):
def __init__(self, name, age, height):
# 调用父类的初始化方法
super().__init__(name, age)
# 定义自己的属性
self.height = height
# 创建学生类
stu = Student('小明', 15, '180')
# 访问属性
print("name: {} age: {} height: {}".format(stu.name, stu.age, stu.height))
# 调用方法
stu.say_hello()
Student
类继承自Person
类
可以使用Person
类中定义的属性name
和age
以及方法say_hello
3. 多态
多态指的是一类事物有多种形态(一个类有多个子类)
多态的概念依赖于继承
例举:
"""
多态案例
"""
# 父类
class Human:
def eat(self):
print('人类吃饭')
# 中国人
class ZhHuman(Human):
def eat(self):
print('中国人使用筷子吃饭')
# 美国人
class UsHuman(Human):
def eat(self):
print('美国人使用刀叉吃饭')
# 非洲人
class AfricaHuman(Human):
def eat(self):
print('非洲人直接用手吃恩希玛')
# 函数
def someone_eat(someone):
'''
接收一个具备吃eat功能的对象
'''
someone.eat()
# 创建四个对象
human = Human()
zh_human = ZhHuman()
us_human = UsHuman()
africa_huamn = AfricaHuman()
# 调用translate方法
someone_eat(human)
someone_eat(zh_human)
someone_eat(us_human)
someone_eat(africa_huamn)
someone_eat方法需要接收具备eat
功能的对象,但是由于ZhHuman
USHuman
AfricaHuman
都具备eat
功能(继承了Human)。所以也可以传递到someone_eat
方法中