美文网首页
Python 29 Programming Tutorial

Python 29 Programming Tutorial

作者: 豆表示低调 | 来源:发表于2016-08-31 11:39 被阅读0次
    Bucky Roberts · September 9, 2014 
    
    class Enemy:
        life = 3
        
        def attack(self):
            print('attack!')
            self.life -= 1
    
    
        def checkLife(self):
            if self.life <= 0:
                print("I am dead")
            else:
                print(str(self.life) + " life left")
    
    
    
    enemy1 = Enemy()
    enemy2 = Enemy()
    
    
    # each object is independent of one another, they don't share variables
    enemy1.attack()
    enemy1.attack()
    enemy1.checkLife()
    enemy2.checkLife()
    

    相关文章

      网友评论

          本文标题:Python 29 Programming Tutorial

          本文链接:https://www.haomeiwen.com/subject/wlaxettx.html