在Python中, __xx__() 的函数叫做魔法方法,指的是具有特殊功能的函数。
体验__init__()
(资料图片)
思考:洗衣机的宽度高度是与生俱来的属性,可不可以在生产过程中就赋予这些属性呢?
答:理应如此。
__init__() 方法的作用:初始化对象。
class Washer(): # 定义初始化功能的函数 def __init__(self): # 添加实例属性 self.width = 500 self.height = 800 def print_info(self): # 类⾥⾯调⽤实例属性 print(f"洗衣机的宽度是{self.width}, ⾼度是{self.height}")haier1 = Washer()haier1.print_info()
注意: __init__() 方法,在创建⼀个对象时默认被调⽤,不需要手动调用 __init__(self) 中的self参数,不需要开发者传递,python解释器会自动把当前的对象引 ⽤传递过去。
带参数的__init__()
思考:⼀个类可以创建多个对象,如何对不同的对象设置不同的初始化属性呢?
答:传参数。
class Washer(): def __init__(self, width, height): self.width = width self.height = height def print_info(self): print(f"洗衣机的宽度是{self.width}") print(f"洗衣机的⾼度是{self.height}")haier1 = Washer(10, 20)haier1.print_info()haier2 = Washer(30, 40)haier2.print_info()
当使用print输出对象的时候,默认打印对象的内存地址。如果类定义了 __str__ 方法,那么就会打印从在这个方法中 return 的数据。
class Washer(): def __init__(self, width, height): self.width = width self.height = height def __str__(self): return "这是海尔洗衣机的说明书"haier1 = Washer(10, 20)# 这是海尔洗衣机的说明书print(haier1)
当删除对象时,python解释器也会默认调用__del__() 方法。
class Washer(): def __init__(self, width, height): self.width = width self.height = height def __del__(self): print(f"{self}对象已经被删除")haier1 = Washer(10, 20)# <__main__.Washer object at 0x000001E02D665588>对象已经被删除del haier1
1.1、需求
需求主线:
被烤的时间和对应的地瓜状态:
0-3分钟:生的
3-5分钟:半生不熟
5-8分钟:熟的
超过8分钟:烤糊了
添加的调料:
用户可以按自己的意愿添加调料
1.2、步骤分析
需求涉及⼀个事物: 地瓜,故案例涉及⼀个类:地瓜类。
1.2.1、定义类
地瓜的属性
被烤的时间 地瓜的状态 添加的调料地瓜的方法
被烤
用户根据意愿设定每次烤地瓜的时间 断地瓜被烤的总时间是在哪个区间,修改地瓜状态添加调料
用户根据意愿设定添加的调料 将用户添加的调料存储显示对象信息
1.2.2、创建对象,调用相关实例方法
1.3、代码实现
1.3.1、定义类
地瓜属性
定义地瓜初始化属性,后期根据程序推进更新实例属性class SweetPotato(): def __init__(self): # 被烤的时间 self.cook_time = 0 # 地⽠的状态 self.cook_static = "⽣的" # 调料列表 self.condiments = []
1.3.2、定义烤地瓜方法
class SweetPotato(): 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 = "烤糊了"
1.3.3、书写str魔法方法,用于输出对象状态
class SweetPotato(): def __str__(self): return f"这个地⽠烤了{self.cook_time}分钟, 状态是{self.cook_static}"
1.3.4、创建对象,测试实例属性和实例方法
digua1 = SweetPotato()print(digua1)digua1.cook(2)print(digua1)
1.3.5、定义添加调料方法,并调用该实例方法
class SweetPotato(): 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)
1.4、代码总览
# 定义类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)
2.1、需求
将小于房子剩余面积的家具摆放到房子中
2.2、步骤分析
需求涉及两个事物:房子和家具,故被案例涉及两个类:房子类和家具类。
2.2.1、定义类
房子类
实例属性
房子地理位置 房子占地面积 房子剩余面积 房子内家具列表实例方法
容纳家具显示房屋信息
家具类
家具名称
家具占地面积
2.2.2、创建对象并调用相关方法
2.3、代码实现
2.3.1、定义类
家具类class Furniture(): def __init__(self, name, area): # 家具名字 self.name = name # 家具占地⾯积 self.area = area
房子类class Home(): def __init__(self, address, area): # 地理位置 self.address = address # 房屋⾯积 self.area = area # 剩余⾯积 self.free_area = area # 家具列表 self.furniture = [] def __str__(self): return f"房⼦坐落于{self.address}, 占地⾯积{self.area}, 剩余⾯积{self.free_area}, 家具有{self.furniture}" def add_furniture(self, item): """容纳家具""" if self.free_area >= item.area: self.furniture.append(item.name) # 家具搬⼊后,房屋剩余⾯积 = 之前剩余⾯积 - 该家具⾯积 self.free_area -= item.area else: print("家具太大,剩余面积不足,⽆法容纳")
2.3.2、创建对象并调用实例属性和方法
bed = Furniture("双⼈床", 6)jia1 = Home("北京", 1200)print(jia1)jia1.add_furniture(bed)print(jia1)sofa = Furniture("沙发", 10)jia1.add_furniture(sofa)print(jia1)ball = Furniture("篮球场", 1500)jia1.add_furniture(ball)print(jia1)
运行效果:
下一篇:最后一页
X 关闭
X 关闭