一、类和对象
class Washer():
def wash(self):
print("用洗衣机来执行洗衣服功能")
haier = Washer()
print(haier)
haier.wash()
二、实例方法
class Washer():
def wash(self):
print("洗衣服")
print(self)
haier = Washer()
geli = Washer()
print(f'第一个对象地址{haier}')
print(f'第二个对象地址{geli}')
haier.wash()
geli.wash()
"""
这里的self指代的就是haier,即self = 调用wash方法的对象
"""
三、类的属性
1.例1
class Washer():
def wash(self):
print("洗衣服")
haier = Washer()
haier.width = 100
haier.height = 150
print(f'第一个属性{haier.width}')
print(f'第一个属性{haier.height}')
2.例2
class Washer():
def wash(self):
print("洗衣服")
def print_info(self):
print(f'第一个属性{self.wdith}')
print(f'第二个属性{self.height}')
haier = Washer()
haier.wdith = 100
haier.height = 150
haier.print_info()
四、魔法方法
1.魔法方法_init_
class Washer():
def __init__(self):
self.width = 100
self.height = 150
def print_info(self):
print(f"属性一{self.width}")
print(f"属性二{self.height}")
haier = Washer()
print(haier.width)
print(haier.height)
haier.print_info()
2.魔法方法_init_(带参)
class Washer():
def __init__(self, width, height):
self.width = width
self.heigth = height
def print_info(self):
print(f'属性一{self.width},属性二{self.heigth}')
haier01 = Washer(width=100, height=150)
haier02 = Washer(300, 450)
haier01.print_info()
haier02.print_info()
3.魔法方法_str_
class Washer():
def __init__(self):
self.width = 100
def __str__(self):
return "这是海尔洗衣机说明书"
haier = Washer()
print(haier)
4.魔法方法_del_
class Washer():
def __init__(self):
self.width = 100
self.height = 150
def __del__(self):
print(f'{self}对象已删除')
haier = Washer()
del haier
5.魔法方法_dict_
class A(object):
a = 0
def __init__(self):
self.b = 1
if __name__ == '__main__':
a = A()
print(A.__dict__)
print(a.__dict__)
五、实例Ⅰ
"""********** 烤地瓜 **********"""
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 8 <= self.cook_time:
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}'
digua01 = SweetPotato()
print(digua01)
digua01.cook(2)
digua01.add_condiments('辣椒面')
print(digua01)
digua01.cook(2)
digua01.add_condiments('老干妈')
print(digua01)
digua01.cook(2)
print(digua01)
六、实例Ⅱ
"""********** 搬家具 ***********"""
class Furniture():
def __init__(self, name, area):
self.name = name
self.area = area
bed = Furniture('水床', 100)
sofa = Furniture('沙发', 80)
bingxiag = Furniture('冰箱', 300)
class Home():
def __init__(self, address, area):
self.address = address
self.area = area
self.free_area = 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('面积容量不足')
def __str__(self):
return f'房子的位置在{self.address},房子的总面积{self.area},剩余面积{self.free_area},家具有{self.furniture}'
jia01 = Home('北京', 250)
print(jia01)
jia01.add_furniture(bed)
print(jia01)
jia01.add_furniture(sofa)
print(jia01)
jia01.add_furniture(bingxiag)
print(jia01)
|