Python类位置变量

时间:2015-05-20 02:59:06

标签: python class variables localization

我想减少球员的健康状况,但我不确定我是否能够做到这一点,对不起,如果这个问题不清楚,但我是一个新的编码员,我会慢慢掌握课程的运作方式。我想我需要尽可能清楚地说明这一点。

class Health(object): 
#health of player and enemy
#input of the both health so it is able to
#be a preset for more combat

    def __init__(self, player, enemy):

        self.player = player
        self.enemy = enemy

class Attack_Type(object):
#making a attack type blueprint
#combat attacks can be modified

    def melee(self, target):
        #100% chance of landing hit
        if target == player:
            Health.player -= 1
            return Health.player
        elif target == enemy:
            Health.enemy -= 1
            return Health.enemy

test = Health(10,10)
enemy_melee = Attack_Type(Health.player)

我的问题是如何在不使全局变量的情况下访问类中的变量值。我能够在类中更改类值的值吗? 这不会改变玩家的健康状况,因为它无法访问玩家的健康状况,但即使这样做也不会将价值返回到正确的位置

我现在意识到将健康变成一个属性要简单得多,对不起每个人我都不完全理解课程的运作方式!谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

我希望这有帮助! :)

class Health:
    def __init__(self): #Constructor initializing the variables.
        self.player_health = 10
        self.enemy_health = 10

class Combat:
    #Attack function receives a health object called "hitter" 
    def attack(self, hitter):
        hitter.player_health -= 1   #Health Object hitter's player_health reduced by one. 
        return hitter

bridge = Combat() #Combat Object
hitter = Health() #Health Object

bridge.attack(hitter) #hitter.player_health is now 9.

答案 1 :(得分:0)

这是一个化身。

public async Task<bool> CheckFile(string file, string filehash) 
{
     await Task.Run<bool>(()=> {
        if (File.Exists(file))
        {

            using (FileStream stream = File.OpenRead(file))
            {
                SHA1Managed sha = new SHA1Managed();
                byte[] checksum = sha.ComputeHash(stream);
                string sendCheckSum = BitConverter.ToString(checksum)
                    .Replace("-", string.Empty);

               return sendCheckSum.ToLower() == filehash;
            }
        }
        else return false;
     });
}