Python多重继承(RPG示例)

时间:2016-01-15 13:06:19

标签: python inheritance

我只是想组建一个简单的RPG示例,以便在Python中学习OOP。

我创建了一个基类CharRace(对象)。然后我创建了两个派生类,Elf(CharRace)和Dwarf(CharRace)。另外,我有第二个基类CharClass(对象),有两个派生类:Wizard(CharClass)和Warrior(CharClass)。

我希望能够做的是创建一个名为NewChar的新类,我可以传递一个名称,一个种族和一个类,它将创建一个继承所有属性的适当对象。比赛和班级。

例如,我想用这样的东西创建一个NewChar的类实例: Player1 = NewChar(" Bob",Elf,Wizard) Player2 = NewChar(" Phil",Dwarf,Warrior)

我确定这是一种智能/正确/可扩展的方式,或者我的课程应该以不同的方式组织,但我找不到它。任何帮助或建议表示赞赏!

class CharRace(object):
    'race for all characters'
    health = 0
    mana = 0
    race = ""
    name = ""
    def __init__(self, health, mana, race, name):
        self.health = health
        self.mana = mana
        self.race = race
        self.name = name
        print ("self = ",self)
    def decreaseHealth (self, amount):
        self.health = self.health - amount
        print ("Decreased health to: ", self.health)
        if (self.health) <= 0:
            print (self.name, "has died!!!")
    def increaseHealth (self, amount):
        self.health = self.health + amount
        print ("Increased health to: ", self.health)
    def printStats(self):
        print()
        print("Stats for",self.name)
        print ("    Race =",self.race)
        print ("    Health =",self.health)
        print ("    Mana =",self.mana)
        CharClass.printStats(self)
    def __str__(self):
        return (self.name)

class CharClass(object):
    'class for all characters'
    weapon = ""
    armor = 0
    def __init__(self, weapon, armor, classname):
        self.weapon = weapon
        self.armor = armor
        self.classname = classname
        print ("self = ",self)   
    def printStats(self):
        print ("    Weapon =",self.weapon)
        print ("    Armor =",self.armor)

# Character Classes
class Warrior(CharClass):
    'warrior class'
    weapon = "Sword"
    armor  = 200
    classname = "Warrior"
    def __init__(self):
        CharClass.__init__(self, self.weapon, self.armor, self.classname)

class Wizard(CharClass):
    'wizard class'
    weapon = "Wand"
    armor  = 100
    classname = "Wizard"
    def __init__(self):
        CharClass.__init__(self, self.weapon, self.armor, self.classname)

# Character Races
class Elf(CharRace):
    'Elf subclass'
    health = 100
    mana = 200
    race = "Elf"
    def __init__(self, name):
        CharRace.__init__(self, self.health, self.mana, self.race, name)

class Dwarf(CharRace):
    'Dwarf subclass'
    health = 200
    mana = 100
    race = "Dwarf"
    def __init__(self, name):
        CharRace.__init__(self, self.health, self.mana, self.race, name)


# This is not an efficient or scalable way to do this....
class ElfWarrior(Elf,Warrior):
    'New character creation'
    def __init__(self, name):
        Elf.__init__(self, name)
        Warrior.__init__(self)

class ElfWizard(Elf,Wizard):
    'New character creation'
    def __init__(self, name):
        Elf.__init__(self, name)
        Wizard.__init__(self)

class DwarfWarrior(Dwarf,Warrior):
    'New character creation'
    def __init__(self, name):
        Dwarf.__init__(self, name)
        Warrior.__init__(self)

class DwarfWizard(Dwarf,Wizard):
    'New character creation'
    def __init__(self, name):
        Dwarf.__init__(self, name)
        Wizard.__init__(self)

1 个答案:

答案 0 :(得分:0)

  

我希望能够做的是创建一个名为NewChar的新类,我可以传递一个名称,一个种族和一个类,它将创建一个继承了两个属性的相应对象。种族和阶级。

如果您真的想使用这种方法,可以尝试这样的方法:

def create_character(race, class, name):
    type_name = race.__name__ + class.__name__
    return type(type_name, (race, class), {'name'=name}

Here是关于类型函数和元类的一个很好的讨论,您可以使用它来进一步微调它。

那就是说,这真的不是最好的方法。首选组合继承。如果你的角色改变了课程,或者你想要双课,怎么办?

您可以尝试这样的事情:

class Character(object):

    def __init__(name, race, class):
        self.name = name
        self.race = race.race
        #etc

    def increase_health(amount):
        self.race.increase_health(amount)

查看设计模式:可重复使用的面向对象软件的元素 Head First Design Patterns 以了解更多信息。