# Baked sweet potatoes Case # 1 is defined classes: initialization properties, roasted and seasoning method for displaying object information str class SweetPotato (): # define potato initialization properties DEF the __init__ (Self): # broiled time self.cook_time = 0 # roasted state self.cook_state = ' raw ' # spices listing self.condiments = [] DEF Cook (Self, time): # baked sweet potatoes the method of # 1 calculates the first whole baked potato time self.cook_time + = time # a whole baked potato and then determine the time state IF 0 <= self.cook_time <. 3 : self.cook_state = ' raw ' elif . 3 <= self.cook_time <. 5 : self.cook_state = ' underdone ' elif . 5 <self.cook_time = <. 8 : self.cook_state = ' cooked ' elif self.cook_time>. 8 : self.cook_state = ' burned the ' DEF add_condiments (Self, condiment): # seasoning sauce is added to the user's intention of listing self.condiments.append (condiment) DEF __str__ (Self): return ' Sweet potatoes baked% s, state% s, spices have S% ' % (self.cook_time, self.cook_state, self.condiments) # 2 creates an object and call the corresponding instance method digua1 = SweetPotato () Print (digua1) # sweet potatoes baked 0, state of health, there seasoning [] digua1.cook (2 ) digua1.add_condiments ( ' sugar ' ) Print (digua1) # sweet potatoes baked 2, raw state, seasoning of [ 'sugar' ] digua1.cook (2 ) digua1.add_condiments ( ' honey ' ) Print (digua1) # sweet potatoes baked 4, a half-baked state, seasoning of [ 'sugar', 'honey'] digua1.cook (2 ) Print (digua1) # Sweet potatoes baked 6, a state is cooked, spices of [ 'sugar', 'honey'] Print ( " - " * 50 ) # moving furniture # Demand: The remainder of the house is smaller than the area of furniture to house # defined class # define furniture class furniture's (): DEF __init__ (Self, name, Area): # product like self.name = name # furniture covers self.area = Area # define class house class house (): DEF __init__ (Self, address, Area): self.address = address self.area= Area self.free_area = Area self.furnitue = [] DEF __str__ (Self): return ' house location is% s, the housing area was% s, remaining area% s, furniture S% ' % (Self. address, self.area, self.free_area, self.furnitue) DEF add_furniture (Self, Item): # accommodate furniture # If the furniture can be moved into an area smaller than the remaining area, furnished a list of names to add furniture, housing the remaining area update, otherwise, can not accommodate IF item.area <= self.free_area: self.furnitue.append (item.name, for) self.free_area - = item.area the else : Print( " Shortage area, does not fit " ) Bed = Furniture's ( ' bed ' , 6 ) Sofa = Furniture's ( ' sofa ' , 10 ) ballchang = Furniture's ( ' basketball court ' , 2000 ) home1 = House ( ' Beijing ' , 1000 ) Print (home1) # house location in Beijing, the housing area is 1000, and the remaining area of 1000, furniture [] home1.add_furniture (Bed) Print (home1) # house location in Beijing, the housing area is 1000, The remaining area 994, furniture [ 'bed'] home1.add_furniture (Sofa) Print(home1) # house location in Beijing, the housing area is 1000, and the remaining area of 984, furniture [ 'bed', 'sofa'] home1.add_furniture (ballchang) Print (home1) # shortage area, to fit in # house the location is Beijing, the housing area is 1000, and the remaining area of 984, furniture [ 'bed', 'sofa']
Object-Oriented Case baked sweet potatoes moving furniture python achieve
Guess you like
Origin www.cnblogs.com/spp666/p/12098781.html
Recommended
Ranking