这是做子类的正确方法吗? [英] is this the right way to do subclasses?
问题描述
好的,回到我所谓的游戏。我只是好奇我是否已经正确地实现了子类,因为看起来像参数的重复次数很多。而且,如果我想稍后添加一个新的
属性,我将不得不改变很多东西。我不能帮助,但是我觉得我的工作非常低效。
谢谢!
class Character(object):
def __init __(自我,名字,力量,灵巧,情报):
self.name = name
self.health = 10
self.strength = strength
self.dexterity = dexterity
self.intelligence =智慧
级战士(角色):
def __init __(自我,名字,力量,灵巧,智力):
Character .__ init __(自我,名字,力量,灵巧,智慧)
self.health + = 2
self.strength + = 1
class小偷(角色):
def __init __(自我,名字,力量,灵巧,智慧):
Character .__ init __(自我,名字,力量,灵巧) ,情报)
self.health + = 1
self.dexterity + = 1
class Mage(Character):
def __init __(self,name,s力量,灵巧,智慧):
性格.__初始__(自我,名字,力量,灵巧,智力)
self.intelligence + = 1
Ok, back to my so-called "game." I''m just curious if I''ve implemented
the subclasses properly, because it seems like an awful lot of
repetition with the parameters. And again, if I want to add a new
attribute later, I''d have to change a lot of things. I can''t help but
get the feeling that I''m doing something very inefficiently.
Thanks!
class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
class Fighter(Character):
def __init__(self, name, strength, dexterity, intelligence):
Character.__init__(self, name, strength, dexterity, intelligence)
self.health += 2
self.strength += 1
class Thief(Character):
def __init__(self, name, strength, dexterity, intelligence):
Character.__init__(self, name, strength, dexterity, intelligence)
self.health += 1
self.dexterity += 1
class Mage(Character):
def __init__(self, name, strength, dexterity, intelligence):
Character.__init__(self, name, strength, dexterity, intelligence)
self.intelligence += 1
推荐答案
John Salerno写道:
John Salerno wrote:
好的,回到我所谓的游戏。我只是好奇我是否已经正确地实现了子类,因为看起来像参数的重复次数很多。而且,如果我想稍后添加一个新的
属性,我将不得不改变很多东西。我不能帮助,但是我觉得我正在做一些非常低效的事情。
Ok, back to my so-called "game." I''m just curious if I''ve implemented
the subclasses properly, because it seems like an awful lot of
repetition with the parameters. And again, if I want to add a new
attribute later, I''d have to change a lot of things. I can''t help but
get the feeling that I''m doing something very inefficiently.
只需在子类的构造函数中接受变量参数,然后将
转发给基类。
类战斗机(角色):
def __init __(自我,* args,** kw):
角色.__ init __(自我,* args,** kw)
self.health + = 2
self.strength + = 1
这种方式,如果你向基类添加一个新参数,你不需要
来更新所有派生类。
-Farshid >
John Salerno写道:
John Salerno wrote:
好的,回到我所谓的游戏。我只是好奇我是否已经正确地实现了子类,因为看起来像参数的重复次数很多。而且,如果我想稍后添加一个新的
属性,我将不得不改变很多东西。我不能帮助,但是我觉得我正在做一些非常低效的事情。
Ok, back to my so-called "game." I''m just curious if I''ve implemented
the subclasses properly, because it seems like an awful lot of
repetition with the parameters. And again, if I want to add a new
attribute later, I''d have to change a lot of things. I can''t help but
get the feeling that I''m doing something very inefficiently.
class Character(object):
def __init __(self,name,strength,dexterity,intelligence ):
self.name = name
self.health = 10
self.strength = strength
self .dexterity = dexterity
self.intelligence = intelligence
class Fighter(Character):
def __init __(自我,名字,力量,灵巧,智慧):
性格.__初始__(自我,名字,力量,灵巧,智慧)
self.health + = 2 >
self.strength + = 1
class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
class Fighter(Character):
def __init__(self, name, strength, dexterity, intelligence):
Character.__init__(self, name, strength, dexterity, intelligence)
self.health += 2
self.strength += 1
避免重复的一种方法:
class Character(object ):
def __init __(自我,名字,力量,灵巧,情报):
self.name = name
self.health = 10
self.strength =力量
self.dexterity = dexterity
self .intelligence = intelligence
self.fix_attributes()
类战斗机(角色):
def fix_attributes(self):
self.health + = 2
self.strength + = 1
您可能需要一个无操作的fix_attributes实现( )在角色
班。
彼得
One way to avoid the repetition:
class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
self.fix_attributes()
class Fighter(Character):
def fix_attributes(self):
self.health += 2
self.strength += 1
You may need a no-op implementation of fix_attributes() in the Character
class.
Peter
Peter Otten写道:
Peter Otten wrote:
避免重复的一种方法:
class Character(object):
def __init __(自我,名字,力量,灵巧,智慧):
self.name = name
self.health = 10
self.strength =力量
self.dexterity = dexterity
self.intelligence = intelligence
self.fix_attributes()
级战士(角色):
def fix_attributes(个体经营):
self.health + = 2
self.streng th + = 1
您可能需要在Character
类中使用fix_attributes()的无操作实现。
Peter
One way to avoid the repetition:
class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
self.fix_attributes()
class Fighter(Character):
def fix_attributes(self):
self.health += 2
self.strength += 1
You may need a no-op implementation of fix_attributes() in the Character
class.
Peter
但是这如何处理统计数据的初始化?我不会
能够使用战斗机创造一个角色而不用做任何事情
角色,对吧?
But how does this take care of the initialization of the stats? I won''t
be able to use Fighter to create a character without doing everything in
Character, right?
这篇关于这是做子类的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!