哪种方式更好? [英] which way is better?

查看:61
本文介绍了哪种方式更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的OO技能尚未完全磨练的地方。你们中的一些人可能会记住我正在努力的小游戏程序。我写了几个

方法来确定一个角色的类,种族等。我的问题

是,我应该把所有这些问题都放在一个角色基类中(所以

所有子类都继承它们,我可以直接从

对象中调用它们,或者我应该将它们作为静态方法放在Validator类型类中?


哪个更好?另外,如果我做了第一个选择,这将如何影响

确定角色是否属于某个类的方法,

假设子类本身特定于每个班级?

例如,巫师班,战士班等等。这意味着我不需要这些特殊方法吗?


我还在试图围绕如何组织这种东西!

This is where my OO skills aren''t quite honed yet. Some of you might
remember the little game program I was working on for fun. I had several
methods written to determine a character''s class, race, etc. My question
is, should I put all those questions in a Character base class (so that
all subclasses inherit them and I can call them straight from the
objects), or should I put them as static methods in a Validator type class?

Which is better? Also, if I did the first choice, how would this affect
the methods that determine whether a character is of a certain class,
assuming that the subclasses themselves are specific for each class?
E.g., wizard class, fighter class, etc. This would mean I wouldn''t need
those particular methods?

I''m still trying to wrap my mind around how to organize this kind of stuff!

推荐答案



" John Salerno" <乔****** @ NOSPAMgmail.com>在留言中写道

新闻:1t ******************** @ rcn.net ...

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:1t********************@rcn.net...
这个我的OO技能还没有得到很好的磨练。你们当中有些人可能会记得我正在努力的小游戏程序。我有几个
方法来确定一个角色的类,种族等等。我的问题是,我应该把所有这些问题都放在一个Character基类中(这样所有的子类)继承它们,我可以直接从
对象中调用它们,或者我应该将它们作为静态方法放在Validator类型的
类中?
This is where my OO skills aren''t quite honed yet. Some of you might
remember the little game program I was working on for fun. I had several
methods written to determine a character''s class, race, etc. My question
is, should I put all those questions in a Character base class (so that
all subclasses inherit them and I can call them straight from the
objects), or should I put them as static methods in a Validator type
class?




那么这些方法有用吗?它们是类似IsType(Class class,

Race race);或者你在谈论类属性?


我想,子类和类属性应该足够了。你能再多说一下你的情况吗?



Well, how do the methods work? Are they something like IsType(Class class,
Race race); or are you talking about class properties?

Subclasses and class properties should be enough, I would think. Could you
explain your situation a little more?


Daniel O''Connell [C#MVP]写道:
Daniel O''Connell [C# MVP] wrote:
John Salerno <乔****** @ NOSPAMgmail.com>在消息中写道
新闻:1t ******************** @ rcn.net ...
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:1t********************@rcn.net...
这个我的OO技能还没有得到很好的磨练。你们当中有些人可能会记得我正在努力的小游戏程序。我有几个
方法来确定一个角色的类,种族等等。我的问题是,我应该把所有这些问题都放在一个Character基类中(这样所有的子类)继承它们,我可以直接从
对象中调用它们,或者我应该将它们作为静态方法放在Validator类型的
类中?
This is where my OO skills aren''t quite honed yet. Some of you might
remember the little game program I was working on for fun. I had several
methods written to determine a character''s class, race, etc. My question
is, should I put all those questions in a Character base class (so that
all subclasses inherit them and I can call them straight from the
objects), or should I put them as static methods in a Validator type
class?



那么,如何这些方法有用吗?它们是类似IsType(Class class,
Race race);或者你在谈论类属性?

我想,子类和类属性应该足够了。你能再解释一下你的情况吗?


Well, how do the methods work? Are they something like IsType(Class class,
Race race); or are you talking about class properties?

Subclasses and class properties should be enough, I would think. Could you
explain your situation a little more?




当然可以。我有一堆这样的方法:


public bool GetIsWizard()

{

if(// code that显示他是巫师)

返回true;

else

返回false;

}


现在,if语句中的代码是检查单选按钮是否已被检查,所以或许我应该将其更改为您所说的属性?



Certainly. I have a whole mess of methods like this:

public bool GetIsWizard()
{
if (//code that shows he''s a wizard)
return true;
else
return false;
}

Now, the code in the if statement is checking whether a radio button is
checked, so perhaps I should change this to a property like you said?




" John Salerno" <乔****** @ NOSPAMgmail.com>在消息中写道

新闻:4K ******************** @ rcn.net ...

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:4K********************@rcn.net...
丹尼尔O''Connell [C#MVP]写道:
Daniel O''Connell [C# MVP] wrote:
" John Salerno" <乔****** @ NOSPAMgmail.com>在消息中写道
新闻:1t ******************** @ rcn.net ...
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:1t********************@rcn.net...
这个我的OO技能还没有得到很好的磨练。你们当中有些人可能会记得我正在努力的小游戏程序。我有几个
方法来确定一个角色的类,种族等等。我的问题是,我应该把所有这些问题都放在一个Character基类中(这样所有的子类)继承它们,我可以直接从
对象中调用它们,或者我应该将它们作为静态方法放在Validator类型的
类中?
This is where my OO skills aren''t quite honed yet. Some of you might
remember the little game program I was working on for fun. I had several
methods written to determine a character''s class, race, etc. My question
is, should I put all those questions in a Character base class (so that
all subclasses inherit them and I can call them straight from the
objects), or should I put them as static methods in a Validator type
class?



那么,如何这些方法有用吗?它们是类似IsType(Class
class,Race race);或者你在谈论类属性?

我想,子类和类属性应该足够了。你能解释一下你的情况吗?


Well, how do the methods work? Are they something like IsType(Class
class, Race race); or are you talking about class properties?

Subclasses and class properties should be enough, I would think. Could
you explain your situation a little more?



当然可以。我有一堆这样的方法:

公共布尔GetIsWizard()
{
if(//代码显示他是巫师)
返回真的;
其他
返回false;
}
现在,if语句中的代码正在检查单选按钮是否被检查,所以也许我应该改变这个属性,就像你说的那样?



Certainly. I have a whole mess of methods like this:

public bool GetIsWizard()
{
if (//code that shows he''s a wizard)
return true;
else
return false;
}

Now, the code in the if statement is checking whether a radio button is
checked, so perhaps I should change this to a property like you said?




嗯,你不会想要一堆属性,比如巫师,战士,鸡 >
Tamer",但两个属性使用类的枚举和一个种族的枚举

可能是合适的。另一种选择是使用子类,然后使用

''''语句来确定类型。


但是,既然你正在写游戏,并且我会假设它可以用于改变类的

字符,我会选择属性,因为它允许你改变b / b
。使用子类将是有限的,因为每次类改变时类都会改变,并且你最终会得到很多

类(HumanWIzard,HumanFighter,LizardWizard)等)。尽管如此,如果不使用

子类,您最终会得到复杂的代码逻辑(如果不同的类\\\>

需要不同的逻辑)。这可能是组合是做事的最佳方式的一种情况,需要考虑一下。


仍然,我会使用一个属性在这里,像我提到的枚举类型

以上。



Well, you wouldn''t want a bunch of properties like "Wizard, Fighter, Chicken
Tamer", but two properties using an enum of classes and an enum of races
might be appropriate. The other option is to use subclasses and then the
''is'' statement to determien the type.

However, since you are writing a game, and I would assume its possilbe for a
character to change classes, I would go with properties since it allows you
to change things. Using subclasses would be limiting since the class would
have to change every time a class changed and you''d end up with alot of
classes(HumanWIzard, HumanFighter, LizardWizard, etc). Still, without using
subclasses you end up with complicated code logic(if different classes\races
require different logic). This might be a situation where composition is the
best way to do things, gotta think about that one a bit.

Still, I would use a property here, with an enum type like I mentioned
above.


这篇关于哪种方式更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆