Record Python object-oriented programming ideas

  Beginner python, in the programming, it should be step by step how to proceed, follow a rational logical thought process, develop good programming habits, hereby facilitate future access to records.

  For example shot villain

  1. Define the main function, the control flow of the entire process:

  def main():

  "" "Is used to control program flow

  pass

  if __name__ == '__main__':

  main()

  2. build framework. Chinese comments with the main function of each step

  def main():

  # 1. Create a villain objects

  # 2. Create a gun target

  # 3. Create a bb bomb target

  # 4. Create bb bomb target

  5. The villain # bb bb bomb was charged in bomb

  6. The villain # bb gun bullet charged

  # 7. Create an enemy target

  # 8. villain up guns

  # 9. villain shot and killed the enemy

  if __name__ == '__main__':

  main()

  3. built along the frame, and then click Design class, create objects

  4. Each wrote a class method, we must comment "" "write feature of this method is doing." "" (Similar to the broad framework), as well as with # realized step by step how to write, how to achieve, and then to write the implementation process.

  """实例"""

  class A:

  def test(self):

  """carry out testing"""

  # 1. Retrieve data

  XXXXXXXX

  # 2 into the formula

  XXXXXXXX

  5. After a step test is habitually

  # Bb bomb test information

  print (dan_jia)

  Information Test gun #

  print (a gun models)

  Bb number of shells is: 1/20

  With a bb gun shells

  This person has a gun

  The complete code

  class Person():

  """人的类"""

  def __init__(self, name):

  self.name = name

  Blood self.hp = 100 # 100

  def add_bullet(self,danjia_temp, bullet_temp):

  # To add bb bb bomb bomb

  danjia_temp.tianjia_bullet (bullet_temp)

  def add_danjia (self, gun_temp, dan_jia_temp):

  Bb gun bullet to add #

  gun_temp.add_danjia (dan_jia_temp)

  def own_gun(self, gun_temp):

  """拿枪"""

  self.gun = gun_temp

  def __str__(self):

  return "This person has a gun% s"% (self.gun.name)

  def shot(self, target):

  "" "Kill the enemy" ""

  self.gun.fire(target)

  def diaoxue (self, shanghai_temp):

  "" "I was hit Diaoxie" ""

  self.hp-=shanghai_temp

  print ( "hit the enemy, the enemy's blood left:% d"% self.hp)

  class Gun():

  "" "Gun type" ""

  def __init__(self, name):

  self.name = name

  self.danjia = None

  def add_danjia (self, dan_jia_temp):

  # Bb gun shells loaded

  self.danjia = dan_jia_temp

  def __str__(self):

  if self.danjia == None:

  return "% s bb gun bullet does not"% (self.name)

  else:

  return "% s bb gun with bullet"% (self.name)

  def fire(self, target):

  "" "Removed from bb bb bomb bomb, and then let the bb bomb hit the enemy." ""

  bb shells shells removed #bb

  zidan_temp = self.danjia.tanchu_bullet ()

  Bb fight the enemy with bombs #

  if zidan_temp:

  zidan_temp.hurt(target)

  class DanJia ():

  "" "BB bullet Class" ""

  def __init__(self, max_number):

  self.max_number = max_number #bb projectile capacity

  self.bullet_list = [] #bb number of current projectile projectile bb

  def tianjia_bullet(self,bullet_temp):

  "Adding bb bb bomb bomb"

  self.bullet_list.append(bullet_temp)

  def __str __ (self): Zhengzhou Women's Hospital http://www.sptdfk.com/

  return "Number bb bb bomb bomb was:% d /% d"% (len (self.bullet_list), self.max_number)

  def tanchu_bullet(self):

  #bb bomb. bomb pop bb

  if self.bullet_list:

  return self.bullet_list.pop()

  else:

  print ( "Unfortunately, there is no bb bomb ...")

  return None

  class Bullet():

  def __init__(self):

  self.shanghai = 10

  def hurt(self, target):

  target.diaoxue (self.shanghai)

  def main():

  # 1. Create a villain objects

  xiaoren = Person("xiaoren")

  # 2. Create a gun target

  A gun type = Gun ( "Model")

  # 3. Create a bb bomb target

  dan_jia = DanJia (20) # properties: bb bb shells shells can hold 20

  # 4. Create bb bomb target

  bullet = Bullet()

  The villain # people playing the loaded bb bb bomb

  xiaoren.add_bullet (dan_jia, bullet)

  6. The villain # bb gun bullet charged

  xiaoren.add_danjia (a gun models, dan_jia)

  # 7. Create an enemy target

  enermy = Person("enermy")

  # Bb bomb test information

  # Print (dan_jia)

  Information Test gun #

  # Print (a gun models)

  # 8. villain up guns

  xiaoren.own_gun (a gun models)

  #tester

  # Print (xiaoren)

  # 9. villain shot and killed the enemy

  xiaoren.shot(enermy)

  if __name__ == '__main__':

  main()


Guess you like

Origin blog.51cto.com/14503791/2476977