类MovableObject(WorldModel.MovableObject,FieldObject): ''''''MovableObject自上一版本以来没有变化。 这个课程的唯一原因是使WorldModel_v2.Player可以访问 WorldModel_v2.FieldObject 。 '''''' 通过 类玩家(WorldModel.Player,MovableObject): ''''' '玩家的新实现。''''' def passBall(自我,权力,队友): #WorldModel_v2.FieldObject.position()应该被调用 myPosition = self.position() #[新实现剪辑] #========= ====================I intentionally abstracted the problem to remove the irrelevantdetails, but here''s a more concrete (though still simplified) example.I hope it is more clear now.George#=============================def worldModelFactory(version):if version < 2: return WorldModel()else: return WorldModel_v2()class WorldModel(object):def __init__(self):self.ourGoal = self.FieldObject(x=-50, y=0)self.theirGoal = self.FieldObject(x=+50, y=0)self.ball = self.MovableObject()self.teammates = [self.Player(i) for i in xrange(1,12)]self.opponents = [self.Player(i) for i in xrange(1,12)]class FieldObject(object):def __init__(self, id=None, x=0, y=0):self.id = idself._pos = (x,y)def position(self):''''''Get or estimate the current position.''''''return self._posclass MovableObject(FieldObject):def speed(self):''''''Get or estimate the current speed.''''''# [implementation snipped]class Player(MovableObject):def passBall(self,power,teammate):''''''Pass the ball to the teammate.''''''# [implementation snipped]class WorldModel_v2(WorldModel):class FieldObject(WorldModel.FieldObject):''''''New implementation of FieldObject.''''''def position(self):# [new implementation snipped]passclass MovableObject(WorldModel.MovableObject, FieldObject):''''''MovableObject didn''t change since the previous version. Theonly reason for this class is to makeWorldModel_v2.FieldObjectaccessible to WorldModel_v2.Player.''''''passclass Player(WorldModel.Player, MovableObject):''''''New implementation of Player.''''''def passBall(self,power,teammate):# WorldModel_v2.FieldObject.position() should be calledmyPosition = self.position()# [new implementation snipped]#============================= 我有意提取问题以删除不相关的细节,但这里有一个更具体(但仍然简化)的例子。我希望现在更清楚了。 I intentionally abstracted the problem to remove the irrelevant details, but here''s a more concrete (though still simplified) example. I hope it is more clear now. <简化版本的George'的exmaple> def worldModelFactory(版本):如果版本< 2:返回WorldModel() else:返回WorldModel_v2()类WorldModel_v1(对象):类Player(对象): def foo(self):传递#v1实现foo()类WorldModel_v2(对象):类Player(WorldModel_v2.Player): def foo(self):传递#v2执行foo () 所以你使用WorldModel_ *类作为命名空间来保存一组可能继承和扩展或重新定义前一个类WorldModel_ *命名空间中的类。这似乎在您的原始帖子中做了您想要的 ,即如果在v1中定义了一个类但在v2中没有定义 v2将只使用v1的实现。默认情况下,WorldModel_v2将从_v1继承 播放器,因此开箱即可正常工作。 所以你应该没事,但我认为你的问题更实际 比适当的OO方式做什么?这在python中有点像sh b b b b b b b b b b b b b。我们喜欢什么是最简单和可读的?所以这里有一些 实用建议(好吧,至少这是我的意图)。 您是否使用* _v1命名约定来实现向后兼容? 向后兼容是一个巨大的痛苦,我注意到你是从.edu地址张贴的b $ b,所以如果这只是你正在工作的东西你自己或小组中的从 代码中删除了版本控制方面。与群组中的其他人交谈更容易。 从您的示例(我过度修剪)看起来您正在使用<boiled down version of George''s exmaple> def worldModelFactory(version): if version < 2: return WorldModel() else: return WorldModel_v2() class WorldModel_v1(object): class Player(object): def foo(self): pass # v1 implementation of foo() class WorldModel_v2(object): class Player(WorldModel_v2.Player): def foo(self): pass # v2 implementation of foo()So you are using the WorldModel_* classes as a namespace to hold aset of classes that might inherit and extend or redefine the previousclasses in a WorldModel_* namespace. This seems to do what you wantedin your original post, namely if a class is defined in v1 but not in v2that v2 would just use v1''s implementation. WorldModel_v2 will inheritPlayer from _v1 by default, so that should work OK out of the box.So you should be fine there, but I think your question is more practicalthan "what is the proper OO way to do it?" which is a bit of a shibbolethin python. We like "what is easiest and readable?" So here are somepractical recommendations (well, at least that''s my intention).Are you using the *_v1 naming convention for backwards compatibility?Backwards compatibility is a giant pain in the ass, I notice you areposting from a .edu address so if this is just something you are workingon by yourself or in a small group drop the versioning aspects from thecode. Talking to the other people in the group is easier.From your example (which I over-pruned) it looks like you are using WorldModel名称空间定义运行迭代的参数 的游戏。 WorldModel下的类别类似于规则/物理定义(MovableObject),团队的坐标-A 球门柱,球队-B球门柱的坐标,球队-A策略(球员) ), 和B队战略(也是球员)。 WorldModel将成为游戏板。 如果是这样的话,让WorldModel成为一块板子 - 将玩家等放在它下面 作为名称空间,并给它一个运行()接受参数的函数。 将玩家衍生物命名为PlayerDumb,PlayerSmart,PlayerAggressive 等,你可能会有更多的目标或物理规则。 实际的主程序看起来像 ob = WorldModel()#standard 100x100 board winner = ob .run(physics = MovableObject,#定义摩擦和重力 team_a_goal =(50,25), team_b_goal =(5,5), team_a_strategy = PlayerDumb, team_b_strategy = PlayerAggressive, ) 打印赢家是,赢家 我写的比我的意思更多,但基本的想法是,当你不需要时,不要使用课程 - 它只是让事情变得更复杂。那个 应该给你更多时间来处理有趣的部分(玩家 策略,我想象)。 -jackdiedthe WorldModel namespace to define parameters for running an iterationof a game. The classes under "WorldModel" are something likethe rules/physics definition (MovableObject), coordinates of the team-Agoalpost, coordinates of the team-B goalpost, team-A strategy (Player),and team-B strategy (also Player). WorldModel would be the gameboard.If so, make WorldModel just a board - drop putting Player etc under itas a namespace, and give it a run() function that takes parameters.Name the Player derivatives as PlayerDumb, PlayerSmart, PlayerAggressiveetc, you''ll probably have a lot more of those than goals or physics rules.The actual main routine would look something likeob = WorldModel() # standard 100x100 boardwinner = ob.run(physics=MovableObject, # defines friction and gravityteam_a_goal=(50,25),team_b_goal=(5,5),team_a_strategy=PlayerDumb,team_b_strategy=PlayerAggressive,)print "Winner is ", winnerI wrote more than I meant to, but the basic idea is don''t use classeswhen you don''t need to - it just makes things more complex. Thatshould give you more time to tackle the interesting parts (playerstrategies, I''d imagine).-jackdied 这篇关于困惑的OO设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 20:16