程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python object application - Comprehensive Application

編輯:Python

List of articles

  • Comprehensive application
    • Roasted sweet potatoes
      • demand
      • Step analysis
        • Defining classes
        • Define how to roast sweet potato
        • Writing str Magic methods , Used to output object status
        • Create objects , Test instance properties and instance methods
        • Define adding seasoning method , And call the instance method
      • Code summary
    • Move furniture
      • demand
      • Step analysis
        • Defining classes
        • Create an object and call instance properties and methods

Comprehensive application

 I've seen the object , Let's see how to use it ???

Roasted sweet potatoes

demand

Demand main line :

  1. Roasted time and corresponding sweet potato state :
    0-3 minute : raw
    3-5 minute : Halfcooked
    5-8 minute : Cooked
    exceed 8 minute : It's burnt
  2. Seasoning added
    Users can add spices according to their wishes

Step analysis

Requirements involve one thing : Sweet potatoes
Involving a class : Sweet potato

  • Properties of sweet potato (__init__:()
    Baking time
    State of sweet potato
    Seasoning added
  • Add seasoning to sweet potato (def Method ():)
    Roasted : The user sets the time for each roast of sweet potato according to their wishes ; Judge the total time in which sweet potato is roasted , Modify sweet potato status
    Add seasoning : The user is more willing to set the added seasoning ; Store the seasoning added by the user
  • Display the material storage of the object (__str__:()

Defining classes

  • Sweet potato attribute
  • Define initialization properties of sweet potato , Later, update instance properties according to the program
class SweetPotato():
def __init__(self):
# Baking time 
self.cook_time = 0
# State of sweet potato 
self.cook_state = ' raw '
# Seasoning list 
self.condiments = []

Define how to roast sweet potato

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 '

Writing str Magic methods , Used to output object status

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)

Create objects , Test instance properties and instance methods

# 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 

Define adding seasoning method , And call the instance method

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 ']

Code summary

# 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 ']

Move furniture

demand

Put furniture smaller than the remaining area of the house into the house

Step analysis

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

Defining classes

  • Furniture
# 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)
  • Houses
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 an object and call instance properties and methods

# 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 ']

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved