SoFunction
Updated on 2024-11-14

Python Object-Oriented Packaging Case Basics Tutorial

seal inside

  • Encapsulation is a great feature of object-oriented programming
  • First Steps in Object-Oriented Programming: Splitting Properties and Methods into an Abstract Class
  • The outside world creates objects using the class and then has the objects call methods
  • The details of the object's methods are encapsulated inside the tired
    Note: An object's attributes can be an object created by another class

I. Needs Analysis of Soldier Assault Cases

Demand:

  • Soldier Zhang Xiaoga has an Ak47.
  • Soldier, can you fire?
  • The qiang is capable of firing bullets.
  • qiang needs to be loaded ---- to increase the number of bullets

According to the requirements, the division of the class, a total of two classes, a soldier class, a qiang class, due to the creation of the soldier class used qiang, so in the creation of the class first create qiang class.

II. Creating the qiang class

Create the qiang class based on the class diagram table above.
model is the model of the qiang, bullet_count is the number of bullets, add_bullet(self, count) is the method of loading bullets, shoot(self) is the method of firing bullets.
coding

class Gun():
    def __init__(self, model):
        # 1. type of qiang
         = model
        # 2. number of bullets, initial value 0
        self.bullet_count = 0
    def add_bullet(self, count):
        self.bullet_count += count
    def shoot(self):
        # 1. Judging the number of bullets
        if self.bullet_count <= 0:
            print("[%s] out of ammo." % )
            return
        # 2. firing bullets, -1
        self.bullet_count -= 1
        # Prompts for launch information
        print("[%s] Chug, chug, chug. [%d]" % (, self.bullet_count))
# 1. create qiang object
ak47 = Gun("Ak47")
# 2. Calling methods
ak47.add_bullet(50)
()

Results.

III. Creation of the soldier class

Assumption: each soldier does not have a qiang
Define attributes with no initial value: theWhen defining properties, if you don't know what initial value to set, you can set it to None

  • The None keyword means nothing.
  • denotes an empty object with no methods or properties, a special constant
  • NNone can be assigned to any variable.
    FIRE methodology needs:
  • Judge whether there is a qiang or not, no way to charge without a gun.
  • Shout a slogan.
  • load
  • shooters

Code:

class Gun():
    def __init__(self, model):
        # 1. type of qiang
         = model
        # 2. number of bullets, initial value 0
        self.bullet_count = 0
    def add_bullet(self, count):
        self.bullet_count += count
    def shoot(self):
        # 1. Judging the number of bullets
        if self.bullet_count <= 0:
            print("[%s] out of ammo." % )
            return
        # 2. firing bullets, -1
        self.bullet_count -= 1
        # Prompts for launch information
        print("[%s] Chug, chug, chug. [%d]" % (, self.bullet_count))
class Soldier():
    def __init__(self, name):
        # 1. Name
         = name
        # 2. qiang = no guns for recruits
         = None
# 1. create qiang object
ak47 = Gun("Ak47")
# 2. Calling methods
ak47.add_bullet(50)
()
# 3. Creation of Soldier Object - Zhang Xiaoga
zhang = Soldier("Zhang Xiaoga")
# Give the ak47 to Zhang Xiaoga with an assignment statement #
 = ak47
print()

Implementation results:

IV. Completion of the firing method

Code:

class Gun():
    def __init__(self, model):
        # 1. type of qiang
         = model
        # 2. number of bullets, initial value 0
        self.bullet_count = 0
    def add_bullet(self, count):
        self.bullet_count += count
    def shoot(self):
        # 1. Judging the number of bullets
        if self.bullet_count <= 0:
            print("[%s] out of ammo." % )
            return
        # 2. firing bullets, -1
        self.bullet_count -= 1
        # Prompts for launch information
        print("[%s] Chug, chug, chug. [%d]" % (, self.bullet_count))
class Soldier():
    def __init__(self, name):
        # 1. Name
         = name
        # 2. qiang = no guns for recruits
         = None
    def fire(self):
        # 1. Determining the presence or absence of a gun
        if  == None:
            print("[%s] not qiang yet" % )
            return
        # 2. Shouting the charge
        print("Charge. [%s]" % )
        # 3. loading
        .add_bullet(50)
        # 4. firing bullets
        ()
# 1. create qiang object
ak47 = Gun("Ak47")
# 2. Calling methods
# ak47.add_bullet(50)
# ()
# 3. Creation of Soldier Object - Zhang Xiaoga
zhang = Soldier("Zhang Xiaoga")
# Give the ak47 to Zhang Xiaoga with an assignment statement #
 = ak47
()
# print()

Implementation results:

Above is the analysis and code walkthrough of the object-oriented case Soldier Assault, the main knowledge point is that the attributes of an object can be another object created by the class, as well as the use of the keyword None, more information about Python object-oriented encapsulation, please pay attention to my other related articles!