How to avoid having the same name for a class and it#39;s namespace, such as 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?
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01