01. Package
- Packaging is a major feature of object-oriented programming
- Object-oriented programming in the first step - the attributes and methods packaged into an abstract class in
- The outside world using a class to create an object , and then let the object calls methods
- Details of the method of the object are encapsulated in the interior of the class
02. Xiao Ming love running
demand
- Xiao Ming weight
75.0
kg - Xiao Ming each runner will lose weight
0.5
kilo - 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
- Xiao Ming and small beauty love running
- Xiao Ming weight
75.0
kg - Little America Weight
45.0
kg - Every run will reduce
0.5
kilograms - Every time eating will increase
1
kg
prompt
- In the interior of the object method , you can directly access the object's properties in
- 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)
03. display furniture
demand
- House (House) have units , the total area and furniture name list
- The new house has no furniture
- 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²
- Simmons (bed) covers an area of
- The above three furniture added to the house in
- When printing house, the required output: apartment , total area , the remaining area , furniture name list
The remaining area
- When creating the house object is defined a property of the remaining area , the initial value is equal to the total area, and
- 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
- Simple furniture
- 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
- Created a furniture , to use
__init__
and__str__
two built-in method - 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
- Creating a house class , to use
__init__
and__str__
two built-in method - Preparing a
add_item
method ready to add furniture - Use house class created a house Object
- Let the house objects called three times
add_item
method, the three furniture to arguments passed to theadd_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