Object-Oriented Encapsulation and Practice in Python

1. encapsulation

# 1. Encapsulating attributes and methods into an abstract class
# 2. External use of classes to create objects and object invocation methods
# 3. Details of object methods are encapsulated inside classes
------> Topic Requirements:
Demand:
1. Li Lei weighs 75.0 kg.
2. Li Lei loses 0.5 kilograms of weight every time he runs.
3. Li Lei gains 1 kilogram every time he eats.
The code is as follows:

class Person():

    def __init__(self,name,weight):
        self.name = name
        self.weight = weight

    def __str__(self):
        return 'My name is %s Weight is %.2f' %(self.name,self.weight)

    def run(self):
        print('%s Love running' %self.name)
        self.weight -= 0.5

    def eat(self):
        print('%s Eat something' %self.name)
        self.weight += 1

Lilei = Person('Li Lei',75.0)
Lilei.run()
print(Lilei)
Lilei.eat()
print(Lilei)

------> Test results:

2. practice

Exercise (1)
----> (1) Topic requirements:
Demand:
1. Household type, total area and furniture name list
The new house has no furniture at all.

2. Furniture has a name and area, of which
Bed: 4 square meters
Wardrobe: 2 square meters
Table: 1.5 square meters
3. Add the above three pieces of furniture to the house
4. When printing a house, output is required: household type, total area, remaining area, furniture name list.
The code is as follows:

class Furniture():

    def __init__(self,name,area):
        self.name = name
        self.area = area

    def __str__(self):
        return '[%s]Land occupation: %.2f' %(self.name,self.area)


# bed = Furniture('bed',4)
# print(bed)

class House():

    def __init__(self,house_type,area):
        self.house_type = house_type
        self.area = area
        #Residual area
        self.free_area = area
        self.item_list = []

    def __str__(self):
        return ('Apartment: %s\n total area: %.2f[Residual area: %.2f]\n furniture:%s'
                %(self.house_type,self.area,self.free_area,self.item_list))

    def add_item(self,item):
        #1. Judging the area of furniture
        if item.area > self.free_area:
            print('%s The area is too large to add.' %item.name)

        #2. Adding Furniture
        self.item_list.append(item.name)

        #3. Calculating Residual Area
        self.free_area -= item.area

bed = Furniture('bed',4)
print(bed)
yigui = Furniture('yigui',200)
print(yigui)
table = Furniture('table',1.5)
print(table)

my_house = House('Single family',400)

my_house.add_item(bed)
my_house.add_item(yigui)
my_house.add_item(table)
print(my_house)

------> Test results:

Exercise (2)
----> (2) Topic requirements:
1. Soldier Ryan has an AK47
2. Soldiers can fire (the trigger is the trigger).
3. Guns can fire bullets.
4. Guns can fill bullets - increase the number of bullets
Simple method:
Soldier Gun

name model
Gun bullet_count enough bullets to finish
The act of shooting.

init(self): init(self):
Fire (self): add_bullet (self, count): bullet loaded
Method
shoot(self):
The code is as follows:

class Gun():

    def __init__(self,model):
        self.model = model
        self.bullet_count = 0

    def add_bullet(self,count):
        self.bullet_count += count

    def shoot(self):
        if self.bullet_count <= 0:
            print('%s No bullets...' %self.model)

        self.bullet_count -= 1
        print('%s ... %s' %(self.model,self.bullet_count))

class Soldier():

    def __init__(self,name):
        self.name = name
        self.gun = None

    def fire(self):
        if self.gun == None:
            print('%s No guns....' %self.name)

        self.gun.add_bullet(5)
        self.gun.shoot()


ak47 = Gun('ak47')
ak47.add_bullet(20)
ak47.shoot()

ryan = Soldier('Ryan')
ryan.gun = ak47
ryan.fire()

------> Test results:

Posted by mikevarela on Fri, 12 Apr 2019 02:03:32 -0700