Python foundation object oriented Foundation

Article directory 1. Furniture - Furniture 2. Furniture - house 3. Place furniture - add furniture 4. Soldier assault - guns 5. Soldiers assault - so...
1. Furniture - Furniture
2. Furniture - house
3. Place furniture - add furniture
4. Soldier assault - guns
5. Soldiers assault - soldiers
6. Soldiers assault - soldiers fire
7. Private properties and methods
8. Pseudo private properties and methods

Article directory

1. Furniture - Furniture

class HouseItem: def __init__(self, name, area): self.name = name self.area = area def __str__(self): return "[%s] Land occupation %.2f" % (self.name, self.area) # 1. Create furniture bed = HouseItem("Simmons", 4) chest = HouseItem("Wardrobe", 2) table = HouseItem("table", 1.5) print(bed) print(chest) print(table)

2. Furniture - house

class HouseItem: def __init__(self, name, area): self.name = name self.area = area def __str__(self): return "[%s] Land occupation %.2f" % (self.name, self.area) class House: def __init__(self, house_type, area): self.house_type = house_type self.area = area # Residual area self.free_area = area # Furniture name list self.item_list = [] def __str__(self): # Python can automatically connect a pair of code inside parentheses return ("Apartment:%s\n Total area:%.2f[Surplus:%.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) # 1. Create furniture bed = HouseItem("Simmons", 4) chest = HouseItem("Wardrobe", 2) table = HouseItem("table", 1.5) print(bed) print(chest) print(table) # 2. Create a house object my_home = House("two bedrooms", 60) my_home.add_item(bed) my_home.add_item(chest) my_home.add_item(table) print(my_home)

3. Place furniture - add furniture

class HouseItem: def __init__(self, name, area): self.name = name self.area = area def __str__(self): return "[%s] Land occupation %.2f" % (self.name, self.area) class House: def __init__(self, house_type, area): self.house_type = house_type self.area = area # Residual area self.free_area = area # Furniture name list self.item_list = [] def __str__(self): # Python can automatically connect a pair of code inside parentheses return ("Apartment:%s\n Total area:%.2f[Surplus:%.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) # 1. Judge the area of furniture if item.area > self.free_area: print("%s The area of is too large to add" % item.name) return # 2. Add the name of the furniture to the list self.item_list.append(item.name) # 3. Calculate the remaining area self.free_area -= item.area # 1. Create furniture bed = HouseItem("Simmons", 40) chest = HouseItem("Wardrobe", 2) table = HouseItem("table", 20) print(bed) print(chest) print(table) # 2. Create a house object my_home = House("two bedrooms", 60) my_home.add_item(bed) my_home.add_item(chest) my_home.add_item(table) print(my_home)

4. Soldier assault - guns

class Gun: def __init__(self, model): # 1. Gun model self.model = model # 2. Number of bullets self.bullet_count = 0 def add_bullet(self, count): self.bullet_count += count def shoot(self): # 1. Judge the number of bullets if self.bullet_count <= 0: print("[%s] No more bullets..." % self.model) return # 2. Fire bullets, - 1 self.bullet_count -= 1 # 3. Prompt launch information print("[%s] Protrusion... [%d]" % (self.model, self.bullet_count)) # 1. Create gun object ak47 = Gun("AK47") ak47.add_bullet(50) ak47.shoot()

5. Soldiers assault - soldiers

class Gun: def __init__(self, model): # 1. Gun model self.model = model # 2. Number of bullets self.bullet_count = 0 def add_bullet(self, count): self.bullet_count += count def shoot(self): # 1. Judge the number of bullets if self.bullet_count <= 0: print("[%s] No more bullets..." % self.model) return # 2. Fire bullets, - 1 self.bullet_count -= 1 # 3. Prompt launch information print("[%s] Protrusion... [%d]" % (self.model, self.bullet_count)) class Soldier: def __init__(self, name): # 1. name self.name = name # 2. Guns - recruits don't have guns self.gun = None # 1. Create gun object ak47 = Gun("AK47") ak47.add_bullet(50) ak47.shoot() # 2. Create Xu Sanduo xusanduo = Soldier("Xu sando") xusanduo.gun = ak47 print(xusanduo.gun)

6. Soldiers assault - soldiers fire

class Gun: def __init__(self, model): # 1. Gun model self.model = model # 2. Number of bullets self.bullet_count = 0 def add_bullet(self, count): self.bullet_count += count def shoot(self): # 1. Judge the number of bullets if self.bullet_count <= 0: print("[%s] No more bullets..." % self.model) return # 2. Fire bullets, - 1 self.bullet_count -= 1 # 3. Prompt launch information print("[%s] Protrusion... [%d]" % (self.model, self.bullet_count)) class Soldier: def __init__(self, name): # 1. name self.name = name # 2. Guns - recruits don't have guns self.gun = None def fire(self): # 1. Judge whether soldiers have guns # if self.gun == None: if self.gun is None: print("[%s] No guns...." % self.name) return # 2. Shout slogans print("Rush!...[%s]" % self.name) # 3. Load the gun self.gun.add_bullet(50) # 4. Let the gun shoot self.gun.shoot() # 1. Create gun object ak47 = Gun("AK47") # 2. Create Xu Sanduo xusanduo = Soldier("Xu sando") xusanduo.gun = ak47 xusanduo.fire() print(xusanduo.gun)

7. Private properties and methods

class Women: def __init__(self, name): self.name = name self.__age = 18 def __secret(self): # Inside the method of an object, you can access the private properties of the object print("%s The age is %d" % (self.name, self.__age)) xiaofang = Women("Xiaofang") # Private property, which cannot be directly accessed outside # print(xiaofang.__age) # Private methods are also not allowed to be accessed directly from outside # xiaofang.__secret()

8. Pseudo private properties and methods

class Women: def __init__(self, name): self.name = name self.__age = 18 def __secret(self): # Inside the method of an object, you can access the private properties of the object print("%s The age is %d" % (self.name, self.__age)) xiaofang = Women("Xiaofang") # Pseudo private property, which cannot be directly accessed outside print(xiaofang._Women__age) # Pseudo private methods are also not allowed to be accessed directly from outside xiaofang._Women__secret()

6 November 2019, 13:33 | Views: 3522

Add new comment

For adding a comment, please log in
or create account

0 comments