1009 jobs

Today's job

1. What is the subject? What is class?

What is the object

The object is a combination of the properties and methods

What is the class

Category is a combination of the same set of object properties and methods

2. What are the characteristics of binding method

  • Invoked by the object, would the object as the first argument passed to the method
  • With the object to call, we will put a different object to the different binding methods

3. Based on object-oriented design a battle game

class Xayah:
    def __init__(self, name, aggr, life, armor):
        self.name = name
        self.aggr = aggr
        self.life = life
        self.armor = armor

    def attack(self, jinx):
        # 自身攻击力-敌方防御力
        damage_value = self.aggr - jinx.armor
        # 敌方生命值-
        jinx.life -= damage_value
        print(f'''
          己方 [{self.name}] 攻击 [{jinx.name}]
          [{jinx.name}]当前生命值为 {jinx.life}
        ''')


class Jinx:
    def __init__(self, name, aggr, life, armor):
        self.name = name
        self.aggr = aggr
        self.life = life
        self.armor = armor

    def attack(self, xayah):
        # 自身攻击力-敌方防御力
        damage_value = self.aggr - xayah.armor
        # 敌方生命值-
        xayah.life -= damage_value
        print(f'''
          己方 [{self.name}] 攻击 [{xayah.name}]
          [{xayah.name}]当前生命值为 {xayah.life}
           ''')


xayah1 = Xayah('霞', 24, 200, 24)
jinx1 = Jinx('金克丝', 20, 240, 20)
while True:

    if xayah1.life <= 0:
        print(f'{xayah1.name}死亡')
        break
    if jinx1.life <= 0:
        print(f'{jinx1.name}死亡')
        break
    jinx1.attack(xayah1)
    xayah1.attack(jinx1)

Guess you like

Origin www.cnblogs.com/faye12/p/11642213.html