python foundation of object-oriented (c) combat: baked sweet potato (SweetPotato)

First, the analysis of "baked potato" properties and methods

Example attributes are as follows:

cookedLevel: This is a number; represented by 0 to 3 is raw, represents more than 3 half-baked, has been baked represents more than 5, more than 8 represents the charcoal has been baked! Our sweet potatoes are born at the beginning of 
cookedString: This is the string; raw and cooked sweet potatoes describe the extent of 
condiments: This is a list of ingredients sweet potatoes, such as ketchup, mustard, etc.

Exemplary methods are as follows:

cook (): the roasted sweet potatoes for some time 
addCondiments (): Add the ingredients to sweet potatoes 
__init __ (): Set the default property 
__str __ (): Let print results look better

Second, the definition of the class, and define __init __ () method

first step:

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
# the Author: Renyz 
# define a class 
class SweetPotato:
     DEF  the __init__ (Self): # initialization, describe the extent potato unfamiliar 
        self.cookedstring = " raw "     # attribute indicates the degree of raw and cooked sweet potatoes, the default is raw 
        self.cookedlevel = 0 # recording sweet potatoes unfamiliar extent 
        # 0 to 3 shows still raw, represents more than 3 half-baked, over five said it had baked More than 8 represents the already baked into charcoal! 
    # Baked sweet potatoes 
    DEF Cook (Self, cooked_time): # cooked_time indicates the length of time 
        IF cooked_time> = 0 and cooked_time <. 3 :
            self.cookedstring = " sweet potato raw "   # . Self represents the attributes of the object change 
        elif cooked_time> =. 3 and cooked_time <. 5 : 
            self.cookedstring = " sweet potato half-baked " 
        elif cooked_time> =. 5 and cooked_time <. 8 : 
            self.cookedstring = " sweet potato cooked " 
        elif cooked_time> 8 : 
            self.cookedstring = " sweet potato burned the " 
# create a sweet potatoes objects 
di_gua = SweetPotato ()
 # call method, start baked sweet potatoes
di_gua.cook (1 )
 Print (di_gua) #   print memory address
The results - >> 
<. __ main __ SweetPotato Object AT 0x000000E2665980F0>

Step two:

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
# the Author: Renyz 
# define a class 
class SweetPotato:
     DEF  the __init__ (Self): # initialization, describe the extent potato unfamiliar 
        self.cookedstring = " raw "     # attribute indicates the degree of raw and cooked sweet potatoes, the default is raw 
        self.cookedlevel = 0 # recording sweet potatoes unfamiliar extent 
        # 0 to 3 shows still raw, represents more than 3 half-baked, over five said it had baked More than 8 represents the already baked into charcoal! 
    # Baked sweet potatoes 
    DEF Cook (Self, cooked_time): # cooked_time indicates the length of time 
        self.cookedlevel + = cooked_time
         IF self.cookedlevel> = 0and self.cookedlevel <. 3 : 
            self.cookedstring   = " sweet potato raw "   # . Self represents the attributes of the object change 
        elif self.cookedlevel> =. 3 and self.cookedlevel <. 5 : 
            self.cookedstring = " sweet potato half-baked " 
        elif Self .cookedlevel> =. 5 and self.cookedlevel <. 8 : 
            self.cookedstring = " sweet potatoes cooked " 
        elif self.cookedlevel>. 8 : 
            self.cookedstring = " sweet potato burned the "
    def __str__(Self): # define str method; what return return to print what 
        return  " sweet potato status:% S (% d) " % (self.cookedstring, self.cookedlevel) 
          # represents the view of the extent and timing grilled # a sweet potato creation Object di_gua = SweetPotato () Print (di_gua) # call method, start baked sweet potatoes di_gua.cook (1 ) Print (di_gua) di_gua.cook ( 1 ) Print (di_gua) di_gua.cook ( 3 ) Print (di_gua)
Results of - >> 
potato state: raw (0) sweet potatoes state: raw sweet potatoes (1) sweet potato state: raw sweet potatoes (2) sweet potatoes state: cooked sweet potatoes (5)

Third, to add seasoning sweet potatoes

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
# the Author: Renyz 
# define a class 
class SweetPotato:
     DEF  the __init__ (Self): # initialization, describe the extent potato unfamiliar 
        self.cookedstring = " raw "     # attribute indicates the degree of raw and cooked sweet potatoes, the default is raw 
        self.cookedlevel = 0 # recording sweet potatoes unfamiliar extent 
        # 0 to 3 shows still raw, represents more than 3 half-baked, over five said it had baked More than 8 represents the already baked into charcoal! 
        = self.condiments [] # properties 
    # baked sweet potatoes 
    DEF Cook (Self, cooked_time): # cooked_time indicates the length of time 
        self.cookedlevel + =cooked_time
         IF self.cookedlevel> = 0 and self.cookedlevel <. 3 : 
            self.cookedstring = " sweet potato raw "   # Self indicates attribute of the object is changed. 
        elif self.cookedlevel> =. 3 and self.cookedlevel <. 5 : 
            Self. cookedstring = " sweet potato half-baked " 
        elif self.cookedlevel> =. 5 and self.cookedlevel <. 8 : 
            self.cookedstring = " sweet potatoes cooked " 
        elif self.cookedlevel>. 8 : 
            self.cookedstring= " Sweet potato burned the " 
    DEF zouliao (Self, items):     # function definition add seasoning 
        self.condiments.append (items) # . The first representation attribute, the second one is added. 
    DEF  __str__ (Self): # define str method; what to print what return return 
        return  " state of sweet potato:% s (% d), there is added a seasoning S% " % (self.cookedstring, self.cookedlevel, str (self.condiments))
         # View grilled represent the extent and timing 
# create a sweet potatoes objects 
di_gua = SweetPotato ()
 Print (di_gua)
 # call method, start baked sweet potatoes 
di_gua.cook (1 )
 Print (di_gua) 
di_gua.zouliao ( "槟榔" ) 
di_gua.cook ( 1 )
 print (di_gua) 
di_gua.zouliao ( " 芥末" ) 
di_gua.cook ( 1 ) 
di_gua.zouliao ( " 芥末" )
 print (di_gua)
Results of - >> 
potato state: raw (0), there is added a seasoning [] potatoes status: raw sweet potatoes (1), there is added a seasoning [] potatoes status: raw sweet potatoes (2 ), there is added a seasoning [ 'betel'] sweet potato state: half-baked sweet potatoes (3), there is added a seasoning [ 'betel', 'mustard', 'mustard']

 

Guess you like

Origin www.cnblogs.com/renyz/p/11592180.html