一 ,面向对象的是实现
1.1 python类的定义
- 语法
class 类名():
代码
#类的写法
class washer():
pass
注意 :在命名类是需要满足标识符命名规则,注意,首字母大写! ,同时要遵循 大驼峰的命名规则
1.2 创建对象
-
语法
对象名 = 类名()
-
示例
# 创建对象
class washer():
pass
he =washer()
print(he)
注意创建对象的过程是叫做实例化对象
1.3 实例属性的初始
-
概述
__init__()`方法的作用:初始化对象。 通俗一点讲 : `__init()__`里面就是**对象属性的"聚集地"** 对象属性既可以在类外面添加和获取,也能在类里面添加和获取。
-
示例
class Washer(): # 定义初始化功能的函数
def __init__(self): # 添加实例属性
self.width = 500 # 宽度是 500
self.height = 800 # 高度 是 800
haier1 = Washer()
print(haier1.width)
class Washer(): # 定义初始化功能的函数
def __init__(self, color,width ,height ,color): # 添加实例属性
self.width = width # 宽度是 500
self.height = height # 高度 是 800
self.yanse = color
haier1 = Washer(500,800,'yellow')
print(haier1.yanse)
注意
- init()`方法,在创建一个对象时默认被调用,不需要手动调用
- init(self)`中的self参数,不需要开发者传递,python解释器会自动把当前的对象引用传递过去。
1.4 self 属性
self指的是调用该函数的对象。
class Washer(): # 定义初始化功能的函数
def __init__(self): # 添加实例属性
self.width = 500 # 宽度是 500
self.height = 800 # 高度 是 800
# print(self)
haier1 = Washer()
print(haier1.width) # 500
# 此时会有两个疑惑 ?
# 1 : __init__ 是一个函数 ,但是为什么没看到调用 ?
# 2 : 函数里面有参数 self ,为什么没看到传参 ?
# 首先看看 self是什么 ? 此时会发现,self 和 haier1 是一样的 所以 ,self就是那个对象
print(haier1) # <__main__.Washer object at 0x0000000000437490>
# 回到那两个疑惑 : 就是在创建对象的时候,Python已经默认调用init函数,并且已经把对象自动的传给self了
# haier1对象调用实例方法
haier1.wash()
haier2 = Washer()
# <__main__.Washer object at 0x0000022005857EF0>
print(haier2)
二 代码实现 烤地瓜
2.1 定义地瓜类
- 地瓜属性 :定义地初始化属性,
class SweetPotato():
def __init__(self):
# 被烤的时间
self.cook_time = 0
# 地瓜的状态
self.cook_static = '生的'
# 调料列表
self.condiments = []
2.2 烤地瓜方法定义
def cook(self, time):
"""烤地瓜的方法"""
self.cook_time += time
if 0 <= self.cook_time < 3:
self.cook_static = '生的'
elif 3 <= self.cook_time < 5:
self.cook_static = '半生不熟'
elif 5 <= self.cook_time < 8:
self.cook_static = '熟了'
elif self.cook_time >= 8:
self.cook_static = '烤糊了'
2.3 书写str魔法方法,用于输出对象状态
def __str__(self):
return f'这个地瓜烤了{self.cook_time}分钟, 状态是{self.cook_static}'
2.4 创建对象,测试实例属性和实例方法
digua1 = SweetPotato()
print(digua1)
digua1.cook(2)
print(digua1)
2.5 定义添加调料方法,并调用该实例方法
def add_condiments(self, condiment):
"""添加调料"""
self.condiments.append(condiment)
def __str__(self):
return f'这个地瓜烤了{self.cook_time}分钟, 状态是{self.cook_static}, 添加的调料有{self.condiments}'
2.6 代码总览
class SweetPotato():
def __init__(self):
# 被烤的时间
self.cook_time = 0
# 地瓜的状态
self.cook_static = '生的'
# 调料列表
self.condiments = []
def cook(self, time):
"""烤地瓜的方法"""
self.cook_time += time
if 0 <= self.cook_time < 3:
self.cook_static = '生的'
elif 3 <= self.cook_time < 5:
self.cook_static = '半生不熟'
elif 5 <= self.cook_time < 8:
self.cook_static = '熟了'
elif self.cook_time >= 8:
self.cook_static = '烤糊了'
def add_condiments(self, condiment):
"""添加调料"""
self.condiments.append(condiment)
def __str__(self):
return f'这个地瓜烤了{self.cook_time}分钟, 状态是{self.cook_static}, 添加的调料有{self.condiments}'
digua1 = SweetPotato()
print(digua1)
digua1.cook(2)
digua1.add_condiments('酱油')
print(digua1)
digua1.cook(2)
digua1.add_condiments('辣椒面儿')
print(digua1)
digua1.cook(2)
print(digua1)
digua1.cook(2)
print(digua1)
digua2 = SweetPotato()
digua2.cook(3)
print(digua2)