I've seen the object , Let's see how to use it ???
Demand main line :
Requirements involve one thing : Sweet potatoes
Involving a class : Sweet potato
__init__:()
)__str__:()
)class SweetPotato():
def __init__(self):
# Baking time
self.cook_time = 0
# State of sweet potato
self.cook_state = ' raw '
# Seasoning list
self.condiments = []
class SweetPotato():
......
# Method for roasting sweet potato
def cook(self,time):
# First calculate the whole baking time of sweet potato
self.cook_time += time
# Judge the state of sweet potato by the time of overall examination
if 0 <= self.cook_time and 3 < self.cook_time:
self.cook_state = ' raw '
elif 3 <= self.cook_time and 5 < self.cook_time:
self.cook_state = ' Halfcooked '
elif 5 <= self.cook_time and 8 < self.cook_time:
self.cook_state = ' Cooked '
else:
self.cook_state = ' It's burnt '
class SweetPotato():
......
# Output the state of the object
def __str__(self):
return ' This sweet potato was roasted for {}, Status is {}'.format(self.cook_time,self.cook_state)
# 2. Create an object and call the method of the instance
digua1 = SweetPotato()
print(digua1) # This sweet potato was roasted for 0, The state is raw
digua1.cook(2)
print(digua1) # This sweet potato was roasted for 2, The state is raw
class SweetPotato():
......
def add_condiments(self,condiment):
# The user's desired seasoning is added to the seasoning list
self.condiments.append(condiment)
digua1 = SweetPotato()
print(digua1) # This sweet potato was roasted for 0, The state is raw , The seasoning for roasted sweet potato is []
digua1.cook(6)
print(digua1) # This sweet potato was roasted for 2, The state is ripe , The seasoning for roasted sweet potato is []
digua1.add_condiments(' chili powder , pepper ')
print(digua1) # This sweet potato was roasted for 6, The state is raw , The seasoning for roasted sweet potato is [' chili powder , pepper ']
digua1.add_condiments(' The soy sauce ')
print(digua1) # This sweet potato was roasted for 6, The state is raw , The seasoning for roasted sweet potato is [' chili powder , pepper ', ' The soy sauce ']
# 1. Defining classes : Initialization property , Method of being roasted and seasoned , Display object information str
class SweetPotato():
def __init__(self):
# Baking time
self.cook_time = 0
# State of sweet potato
self.cook_state = ' raw '
# Seasoning list
self.condiments = []
# Method for roasting sweet potato
def cook(self,time):
# First calculate the whole baking time of sweet potato
self.cook_time += time
# Judge the state of sweet potato by the time of overall examination
if 0 <= self.cook_time or 3 > self.cook_time:
self.cook_state = ' raw '
elif 3 <= self.cook_time and 5 > self.cook_time:
self.cook_state = ' Halfcooked '
elif 5 <= self.cook_time and 8 > self.cook_time:
self.cook_state = ' Cooked '
else:
self.cook_state = ' It's burnt '
def add_condiments(self,condiment):
# The user's desired seasoning is added to the seasoning list
self.condiments.append(condiment)
# Output the state of the object
def __str__(self):
return ' This sweet potato was roasted for {}, Status is {}, The seasoning for roasted sweet potato is {}'.format(self.cook_time,self.cook_state,self.condiments)
# 2. Create an object and call the method of the instance
digua1 = SweetPotato()
print(digua1) # This sweet potato was roasted for 0, The state is raw , The seasoning for roasted sweet potato is []
digua1.cook(6)
print(digua1) # This sweet potato was roasted for 2, The state is ripe , The seasoning for roasted sweet potato is []
digua1.add_condiments(' chili powder , pepper ')
print(digua1) # This sweet potato was roasted for 6, The state is raw , The seasoning for roasted sweet potato is [' chili powder , pepper ']
digua1.add_condiments(' The soy sauce ')
print(digua1) # This sweet potato was roasted for 6, The state is raw , The seasoning for roasted sweet potato is [' chili powder , pepper ', ' The soy sauce ']
Put furniture smaller than the remaining area of the house into the house
Requirements involve two things : House and furniture , Therefore, the case involved two categories : House and furniture .
Houses
Instance attributes : House location , Covers an area of , The remaining area , List of furniture in the house
Example method : Accommodating furniture
Display house information
Furniture
Furniture name
Furniture floor area
# Create a furniture class
class Furniture():
def __init__(self,name,area):
# Furniture name
self.name = name
# Furniture area
self.area = area
# establish
bad = Furniture(' Double bed ',6)
safa = Furniture(' Sofa ',10)
class Home():
def __init__(self,address,area):
# Location
self.address = address
# Building area
self.area = area
# The remaining area
self.free_area = area
# Furniture list
self.furniture = []
def __str__(self) -> str:
return f' The house is located in {
self.address}, The floor area is {
self.area}, The remaining area {
self.free_area}, The furniture we own is {
self.furniture}'
# Create a house class
jia1 = Home(' Chongqing ',1000)
print(jia1) # The house is located in Chongqing , The floor area is 1000, The remaining area 1000, The furniture we own is []
# Create a furniture class
class Furniture():
def __init__(self,name,area):
# Furniture name
self.name = name
# Furniture area
self.area = area
class Home():
def __init__(self,address,area):
# Location
self.address = address
# Building area
self.area = area
# The remaining area
self.free_area = area
# Furniture list
self.furniture = []
def __str__(self) -> str:
return f' The house is located in {
self.address}, The floor area is {
self.area}, The remaining area {
self.free_area}, The furniture we own is {
self.furniture}'
def add_furniture(self,item):
''' Accommodating furniture '''
if self.free_area >= item.area:
self.furniture.append(item.name)
# The remaining area changes
self.free_area -= item.area
else:
print(' The furniture is too big , It's too big ')
# Create a house class
jia1 = Home(' Chongqing ',1000)
print(jia1) # The house is located in Chongqing , The floor area is 1000, The remaining area 1000, The furniture we own is []
# Create furniture
bad = Furniture(' Double bed ',6)
safa = Furniture(' Sofa ',10)
lanqiuchang = Furniture(' Basketball Court ',10000)
jia1.add_furniture(bad)
print(jia1) # The house is located in Chongqing , The floor area is 1000, The remaining area 1000, The furniture we own is [' Double bed ']
jia1.add_furniture(safa)
print(jia1) # The house is located in Chongqing , The floor area is 1000, The remaining area 1000, The furniture we own is [' Double bed ', ' Sofa ']
jia1.add_furniture(lanqiuchang)
print(jia1) # The house is located in Chongqing , The floor area is 1000, The remaining area 1000, The furniture we own is [' Double bed ', ' Sofa ']