如何避免类与其命名空间同名,例如 Technology.Technology?
我经常在命名空间和该命名空间中的类之间遇到命名冲突,并且想知道除了添加随机前缀之外,在首先使用这些名称似乎有意义时处理此问题的最佳实践。
拥有一个 Models.Technology 命名空间似乎很有意义,其中包含数十个技术类(即武器、农业、回收技术)和一些相关接口。 我还决定在 Technology 命名空间中创建一个 Technology 抽象类,所有技术均从中派生。
然而,这迫使我使用这样的代码:
public Technology.Technology research(Technology.Technology tech) {...}
同样:
public Building.Building build(int count) {...}
顺便说一句,我没有命名我的命名空间技术,因为我在其他地方使用该术语作为可用技术列表的包装器......
I am frequently having naming conflicts between a namespace and a class within that namespace and would like to know a best practice for handling this when it seems to make sense to use these names in the first place, besides adding random prefixes.
It seemed to make sense to have a Models.Technology namespace, with dozens of technology classes (ie. Weapon, Agricultural, Recycling technologies) and some related interfaces in it. I also decided to have a Technology abstract class, in the Technology namespace, from which all technologies derive.
However, this is forcing me to use code like this:
public Technology.Technology research(Technology.Technology tech) {...}
and likewise:
public Building.Building build(int count) {...}
By the way I did not name my namespace Technologies since I use that term elsewhere as a wrapper for a list of available technologies...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Technology.Tech
或:
HumanBeing.Human
或:
CocaCola.Coke 怎么样?
我喜欢做的是制定一个简单的规则。 我使用单词的前两个字母,后跟该单词的最后两个字母,例如:
Technology.Tegy
它可能没有任何意义,但由于始终适用相同的规则,因此意味着更少的混乱,并且无需记得很多,除了那一条规则。 这只是我的偏好。 我本人喜欢事物看起来神秘,所以如果这不是你的风格 - 那么这个答案可能不适合你。
我希望这有帮助...至少有一点...:)
What about Technology.Tech
or:
HumanBeing.Human
or:
CocaCola.Coke ?
What I like to do is have a simple rule. Where I use the first two letters of a word, followed by the last two letters of that word like:
Technology.Tegy
It may not make any sense, but because the same rule always applies, it means less confusion, and there's no need to remember much, except for that one rule. This is just my preference. I, myself like things to look cryptic, so if that's not your style - then maybe this answer isn't for you.
I hope this helps... Atleast a little... :)
另一种解决方法是,只需在您工作的公司名称后调用命名空间即可。
Another cope out, just call the namespace after the name of the company you are working for.
一种常见的逃避方法是将您的名称空间命名为“实体”或其他名称。
One general cop-out is to name your namespace "Entities" or something.
好吧,无论您将名称空间命名为 nsMyNamespace,还是将类命名为 cMyClasse。
我个人赞成命名名称空间,因为它不是您在代码中经常使用的东西。
您还可以使用 TechnologyBase 或 AbstractTechnology 来命名与命名空间名称冲突的主要抽象类,这是我建议保持最符合逻辑的命名约定。
Well, whether your name your namespaces like that nsMyNamespace or you name you class cMyClasse.
I'm personnaly in favor of naming the namespace, because it's not something you use a lot in code.
You can also use TechnologyBase or AbstractTechnology for naming your main abstract class that conflict with the name of your namespace, which is what i recommend to keep the most logical naming convention.
如果没有更准确的指示来说明您何时遇到此问题,则很难为您提供一些一般性建议。
但我假设当你的命名空间中有一个基类,并且其他类继承自它时,你就会遇到这个问题。 如果是这种情况,您可以将您的类称为 BaseTechnology(基类)或 AbstractBuilding(抽象类)或 IAnimal(接口)。 这甚至可以让您更加清晰,因为您在类型名称中指定了一些重要的内容。
Without more precise indications on when you encounter this problem it's hard to give you some general advice.
But I'm gonna suppose you have this problem when you have a base class in your namespace, with others classes inheriting from it. If that's the case you could call your class BaseTechnology (base class) or AbstractBuilding (abstract class) or IAnimal (interface). This even give you more clarity since you specify in the name of your type something important about it.