如何避免一个类和它的命名空间同名,例如 Technology.Technology?

How to avoid having the same name for a class and it#39;s namespace, such as Technology.Technology?(如何避免一个类和它的命名空间同名,例如 Technology.Technology?)

本文介绍了如何避免一个类和它的命名空间同名,例如 Technology.Technology?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在命名空间和该命名空间中的类之间发生命名冲突,并且想知道在首先使用这些名称似乎有意义时处理此问题的最佳实践,除了添加随机前缀.

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.

拥有一个 Models.Technology 命名空间似乎很有意义,其中包含数十个技术类(即武器、农业、回收技术)和一些相关接口.我还决定在 Technology 命名空间中创建一个 Technology 抽象类,所有技术都从该类派生.

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) {...}

同样:

public Building.Building build(int count) {...}

顺便说一下,我没有命名我的命名空间 Technologies,因为我在其他地方使用该术语作为可用技术列表的包装器......

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...

推荐答案

如果没有更准确的指示来说明您何时遇到此问题,很难给您一些一般性的建议.

Without more precise indications on when you encounter this problem it's hard to give you some general advice.

但是我会假设当您的命名空间中有一个基类并且其他类从它继承时,您会遇到这个问题.如果是这种情况,您可以调用您的类 BaseTechnology(基类)或 AbstractBuilding(抽象类)或 IAnimal(接口).这甚至可以让您更加清晰,因为您在类型的名称中指定了一些重要的内容.

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.

这篇关于如何避免一个类和它的命名空间同名,例如 Technology.Technology?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何避免一个类和它的命名空间同名,例如 Technology.Technology?