问题描述
我工作的一个项目,在C#W / XNA,我想重新组织和划分它的部分为类。
I'm working on a project in C# w/ XNA, and I want to reorganize it and divide parts of it into classes.
目前,它只是1 game.cs文件,但我想清理一下。不幸的是,我不是太熟悉的类。也许有人可以帮我找出如何做到这一点,或直接我某种一些教程?
At present, it's just one game.cs file, but I want to clean it up a bit. Unfortunately, I'm not too familiar with classes. Perhaps someone can help me figure out how to do this, or direct me to some tutorial of some sort?
推荐答案
好吧,开始关闭的思考,其中的数据位自然一起去。例如,如果你有一个球员的名字,成绩,地位,生活等号,建议你应该有一个播放器
类。所以,看看所有的国家,最多将其分成不同的组,然后检查其现有的方法只能用一组状态或其他工作......并把与他们感兴趣的状态的方法。
Well, start off by thinking of which bits of data go together naturally. For example, if you have a player's name, score, position, number of lives etc, that would suggest you should have a Player
class. So take a look at all the state, split it up into different groups, and then check which of your existing methods only work with one group of state or another... and put the methods with the state they're interested in.
它变得棘手时,一种方法需要从多个对象处理状态,当然:通常是指你把代码中的一种类型,并通过其他类型。作为一个参数
It gets trickier when one method needs to deal with state from multiple objects, of course: typically that means you'll put the code in one type and pass the other type in as a parameter.
很多,这是内心的感觉,说实话 - 以上只是起点。随着时间的推移它变得比较容易,你可能会开始考虑更多方面的责任的不仅仅是数据 - 但它是一个很好的起点,因为你可能已经拥有的数据,你需要的,只是不以正确的方式组织......而在系统内决定的责任可以有很多困难。
A lot of this is gut feeling, to be honest - the above are only starting points. Over time it gets somewhat easier, and you'll probably start to think more in terms of responsibilities than just data - but it's a good starting point, as you probably already have the data you need, just not organized in the right way... whereas deciding on the responsibilities within the system can be a lot harder.
这篇关于我怎样才能重新安排我的代码到C#类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!