学习目标:
掌握面向对象
学习内容:
# 面向对象是非常重要的 抽象 思想结构
# 小明 小红 小雨 人
# 海尔洗衣机 海东洗衣机 海西洗衣机 都是洗衣机
# 猫 狗 熊猫 动物
# 蓝图
# 特征是属性
# 行为是方法
# 对象是继承类的
# print(type(WashingMachine))
# 现有类,再有对象
# 做一张蓝图 生成洗衣机
# class WashingMachine(): # 洗衣机
# width = 595
# heigth = 850
# # 会洗衣服
#
# def canDoLaundry(self):
# print('会洗衣服')
# haier = WashingMachine()
# print(haier.width)
# haier.heigth = 800
# print(haier.height)
# haier.color = 'red'
# print(haier.color)
# haier.canDoLaundry()
# class 英雄
# 魔法方法 被动技能 自动生效 需要某个条件触发
# 魔法方法__xxx__():
# class WashingMachine(): # 洗衣机类
# width = 595
# height = 850
#
# def __init__(self,color): # 初始化属性
# self.color = color
# pass
#
# # 会洗衣服
# def canDoLaundry(self):
# print('会洗衣服')
#
# haier = WashingMachine('red')
# print(haier.color)
# class WashingMachine(): # 洗衣机类
# def __init__(self,width,height):
# self.width = width
# self.height = height
#
# def __add__(self, other): # 当使用+触发
# self.width += other
# self.height += other
# return self
#
# def __del__(self): #
# print('触发del')
#
# def __str__(self): # 当使用print触发
# """海尔说明书"""
# return f'高度为:{self.width}\n宽度为:{self.height}'
#
#
# haier = WashingMachine(590,850)
# haier + 2 + 3
# print(haier)
# 烤地瓜 红薯
# class SweetPotato:
#
# def __init__(self, times, status, seasoning=[]): # 在使用时触发
# self._times = times
# self._status = status
# self._seasoning = seasoning # 调料
#
# def add_seasoning(self, *season): # 增加调料
# for i in season:
# self._seasoning.append(i)
#
# def roastedSweetPotato(self, times: int): # 烤红薯
# # 2-2分钟 生的 2-5 半生 5-8 刚刚好 8-烤焦了
# self._times += times
# if 2 >= self._times >= 0:
# self._status = '生的'
# elif 5 >= self._times >= 3:
# self._status = '半生的'
# elif 8 >= self._times >= 6:
# self._status = '刚刚好的'
# elif self._times >= 9:
# self._status = '烤焦了的'
#
# def __str__(self): # 查看红薯状态
# return f'红薯烤的时间:{self._times}分钟\n' \
# f'红薯的状态是:{self._status}\n红薯目前的调料:' \
# f'{" ".join(self._seasoning)}'
#
#
# sweet1 = SweetPotato(0, '生的', [])
# sweet1.add_seasoning('番茄酱', '孜然')
# sweet1.roastedSweetPotato(3)
# sweet1.roastedSweetPotato(2)
# sweet1.roastedSweetPotato(1)
# print(sweet1)
#
# sweet2 = SweetPotato(2,'生的',['孜然'])
# 私有属性 在属性名称的前面加一个下划线_
课后作业:
class MyTime:
def __init__(self):
self.hour = 0
self.minute = 0
self.second = 0
def get(self):
"""获取时间"""
print(f'现在是{self.hour}时{self.minute}分{self.second}秒')
def set(self, hour, minute, second):
"""设置时间"""
self.hour = hour
self.minute = minute
self.second = second
def __str__(self):
return f'现在是{self.hour}时{self.minute}分{self.second}秒'
time = MyTime()
print(time)
time.get()
time.set(9, 31, 0)
print(time)
学习产出:
- 技术笔记 1遍