Object-oriented package case Python

01. Package

  1. Packaging  is a major feature of object-oriented programming
  2. Object-oriented programming in  the first step  - the  attributes  and  methods  packaged  into an abstract  class  in
  3. The outside world  using a  class  to create  an object , and then  let the object calls methods
  4. Details of the method of the object  are  encapsulated  in the  interior of the class

02. Xiao Ming love running

demand

  1. Xiao Ming  weight 75.0  kg
  2. Xiao Ming each  runner  will lose weight  0.5 kilo
  3. Xiao Ming each  eating  weight gain  1 kg

Note: In  -house method object , it can  directly access the object's properties  in!

class the Person:
     DEF  __init__ (Self, name, weight): 
        self.name = name 
        self.weight = weight 

    DEF RUN (Self): 
        self.weight - = 5
         Print ( " % S love jogging, jogging exercise " % Self. name) 

    DEF eAT (Self): 
        self.weight + 1 = Print ( " % s is a food goods, eating this meal and then lose weight " % self.name) DEF __str__ (Self):
         return " my name is% s% body weight .2f kg " %

        

       (self.name, self.weight)


xiaoming = Person('小明',75)
xiaoming.eat()
xiaoming.eat()
xiaoming.eat()
print(xiaoming)

2.1 Caritas running small extension - Little America love running

demand

  1. Xiao Ming  and  small beauty  love running
  2. Xiao Ming  weight 75.0  kg
  3. Little America  Weight 45.0  kg
  4. Every  run  will reduce  0.5 kilograms
  5. Every time  eating  will increase  1 kg

prompt

  1. In the  interior of the object method , you can  directly access the object's properties  in
  2. The same class  to create  multiple objects  between the property  without disturbing each other!
class the Person: 

    DEF  __init__ (Self, name, weight): 

        # . Self attribute = parameter 
        self.name = name 
        self.weight = weight 

    DEF  __str__ (Self): 

        return  " My name is% s% .2f kg body weight " % (self.name, self.weight) 

    DEF RUN (Self):
         Print ( " % S love jogging, jogging exercise " % self.name) 

        self.weight - = 0.5 DEF EAT (Self):
         Print ( " % s is a food goods, eating this meal and then lose weight " % self.name)

    

        self.weight += 1

xiaoming = Person("小明", 75.0)

xiaoming.run()
xiaoming.eat()

print(xiaoming)

# 小美爱跑步
xiaomei = Person("小美", 45)

xiaomei.eat()
xiaomei.run()

print(xiaomei)
print(xiaoming)
View Code

03. display furniture

demand

  1. House (House)  have  units , the total area  and  furniture name list
    • The new house has no furniture
  2. Furniture (HouseItem)  have  names  and  covers an area where
    • Simmons (bed)  covers an area of  4 square meters
    • Wardrobe (chest)  area  2 square meter
    • Table (table)  area  1.5 m²
  3. The above three  furniture  added  to the  house  in
  4. When printing house, the required output: apartment , total area , the remaining area , furniture name list

The remaining area

  1. When creating the house object is defined a  property of the remaining area , the initial value is equal to the total area, and
  2. When you call the  add_item method, to the room  to add furniture  , the let  the remaining area  - =  furniture area

Reflection : Which class should first develop?

The answer  -  Furniture

  1. Simple furniture
  2. To use the house to the furniture, to be used in class , generally should first develop

3.1 Creating furniture

class HouseItem: 

    DEF  __init__ (Self, name, Area):
         "" " 

        : param name: Furniture Name 
        : param area: an area 
        " "" 
        self.name = name 
        self.area = Area 

    DEF  __str__ (Self):
         return  " [% s] .2f area% " % (the self.name, self.area) 


# 1. Create furniture 
Bed HouseItem = ( " Simmons " ,. 4 ) 
Chest = HouseItem ( " closet " , 2 ) 
Table = HouseItem ("餐桌", 1.5)

print(bed)
print(chest)
print(table)

summary

  1. Created a  furniture , to use  __init__ and  __str__ two built-in method
  2. Use  furniture  to create  three furniture objects , and  output information Furniture

3.2 Creating Rooms

class House: 

    DEF  the __init__ (Self, house_type, Area):
         "" " 

        : param house_type: Unit 
        : param area: total area of 
        " "" 
        self.house_type = house_type 
        self.area = Area 
        
        # remaining area and the total area consistent with the default 
        self. = free_area area
         # default without any furniture 
        self.item_list = [] 

    DEF  __str__ (Self): 

        # the Python code can be automatically pair of inner brackets connected 
        return ( " Unit:% s \ n total area:%. . 2F [remaining:% 2f.] \ n furniture:% S " 
                %(self.house_type, self.area, 
                   self.free_area, self.item_list)) 

    DEF add_item (Self, Item): 

        Print ( " To add S% " % Item) 

... 

# 2. Create a house objects 
my_home = House ( " Bedroom " , 60 ) 

my_home.add_item (Bed) 
my_home.add_item (Chest) 
my_home.add_item (Table) 

Print (my_home)

summary

  1. Creating a  house class , to use  __init__ and  __str__ two built-in method
  2. Preparing a  add_item method  ready to add furniture
  3. Use  house class  created  a house Object
  4. Let  the house objects  called three times  add_item method, the  three furniture  to arguments passed to the  add_item interior

3.3 Adding furniture

demand

  • 1>  Analyzing  furniture area  whether or  exceeds the remaining area , if exceeded , can not be prompted to add the piece of furniture
  • 2> The  name of the furniture  added to the  furniture list of names  of
  • 3> with  the remaining area of the house  -  furniture area
DEF add_item (Self, Item): 

        Print ( " To add% s " % Item)
         # 1. Furniture determines whether the area greater than the remaining area 
        IF item.area> self.free_area:
             Print ( " area% s is too large, can not be added to house the " % item.name) 

            return 

        # 2. the name of the furniture is appended to the name list 
        self.item_list.append (item.name) 

        # 3. calculates the remaining area 
        self.free_area - = item.area

3.4 Summary

  • The main program is only responsible for creating  house  objects and  furniture  objects
  • Let  the house  object call  add_item method  will be added to the house furniture in
  • Area calculations , the remaining area , furniture list  and other processing are  encapsulated  into the  interior of the house class

Guess you like

Origin www.cnblogs.com/huiyichanmian/p/11279831.html