七、面向对象_4.例题

泡泡面

0 - 2 不熟
2 - 4 一点熟
4 - 6 完全熟
6 -   过熟

class InstantNooddles():
    def __init__(self):
        self.soak_time = 0
        self.D = ''
        self.S = []

    def Soak(self,time):
        self.soak_time += time
        if 0 < self.soak_time <= 2 :
            self.D = '不熟'
        elif 2 < self.soak_time <= 4 :
            self.D = '半熟'
        elif 4 < self.soak_time <= 6 :
            self.D = '全熟'
        elif 6 < self.soak_time :
            self.D = '过熟'

    def AddSeasoning(self,seasoning):
        self.S.append(seasoning)

    def __str__(self):
        return f'{self.S if self.S != [] else "没有加调料"},泡了{self.soak_time}分钟,{self.D}'

Soup = InstantNooddles()
Soup.AddSeasoning('pepper')
Soup.AddSeasoning('egg')
Soup.Soak(2)
print(Soup)
Soup.Soak(2)
print(Soup)

['pepper', 'egg'],泡了2分钟,不熟
['pepper', 'egg'],泡了4分钟,半熟

搬家具

class Furniture():
    def __init__(self,name,area):
        self.f_name = name
        self.f_area = area

sofa = Furniture('沙发',6)
desk = Furniture('桌子',6)
bed = Furniture('床',12)
refrigerator = Furniture('冰箱',3)
kitchen = Furniture('厨房',12)
door = Furniture('门',2)

class House():
    def __init__(self,position,area):
        self.h_position = position
        self.h_area = area
        self.residual_area = area
        self.furnitureList = []

    def add_furniture(self,f):
        if self.residual_area >= f.f_area :
            self.furnitureList.append(f.f_name)
            self.residual_area -= f.f_area
        else :
            print(f'空间不足,需要空间:{f.f_area},剩余空间:{self.residual_area}')

    def __str__(self):
        return f'{self.h_position},面积:{self.h_area},已放入{self.furnitureList},剩余空间:{self.residual_area}'

house1 = House('后海',40)
house1.add_furniture(sofa)
house1.add_furniture(desk)
house1.add_furniture(bed)
house1.add_furniture(refrigerator)
house1.add_furniture(kitchen)
house1.add_furniture(door)
print(house1)


空间不足,需要空间:2,剩余空间:1
后海,面积:40,已放入['沙发', '桌子', '床', '冰箱', '厨房'],剩余空间:1
上一篇:热点地图------map


下一篇:OSPF 之 不规则区域