为抽象类添加前缀“A” 类似的接口都以“I”为前缀?

发布于 2024-07-16 17:04:24 字数 277 浏览 8 评论 0原文

在我看来,能够一眼看出类是抽象的并且不意味着直接实例化是有用的,就像能够轻松识别接口一样有用。

我的问题是,为什么“AFourLeggedAnimal:IAnimal”没有流行起来? 仅仅是因为可能的混淆(我在写这篇文章时刚刚注意到),例如将其混淆为“四足动物”而不是“抽象类 FourLeggedAnimal”? 或者还有其他什么?

从学校里的 Java 到工作中的 C#,我发现“I”前缀命名约定在浏览类列表时非常有用,而且在我看来,能够区分具体和非具体会很方便类一目了然,无需查看代码。

It seems to me that it'd be useful to be able to tell at a glance that a class is abstract and not meant to be directly instantiated just like it's useful to be able to easily identify an interface.

My question is, why didn't "AFourLeggedAnimal : IAnimal" catch on? Is it simply because of the possible confusion (which I just noticed while writing that) for example confusing it as "A four legged animal" instead of "abstract class FourLeggedAnimal"? Or is it something more?

Coming from Java in school to C# at work, I found the "I" prefix naming convention extremely useful when glancing through a list of classes and it seems to me that it'd be convenient to be able to distinguish between concrete and non-concrete classes at a glance without needing to look at the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏天碎花小短裙 2024-07-23 17:04:24

正如乔尔提到的那样,使用后缀“Base”。 一旦你的项目中有很多内容,就很容易区分:

public abstract AnimalBase
{
  public AnimalType AnimalType { get; set; }
}

public abstract HorseBase : AnimalBase
{
  public HoovesManufacturer HoovesManufacturer { get; set; }
}

public class Pony : HorseBase
{
  public Pony()
  {
  }
}

Use the suffix "Base" as Joel mentions. Once you have a lot in your project, it's pretty easy to tell apart:

public abstract AnimalBase
{
  public AnimalType AnimalType { get; set; }
}

public abstract HorseBase : AnimalBase
{
  public HoovesManufacturer HoovesManufacturer { get; set; }
}

public class Pony : HorseBase
{
  public Pony()
  {
  }
}
菩提树下叶撕阳。 2024-07-23 17:04:24

我更喜欢带有“Base”后缀。

I prefer to suffix with "Base".

二智少女猫性小仙女 2024-07-23 17:04:24

因为,坦率地说,命名抽象类的可接受模式已经设定;-) 它带有“Base”后缀,如 MyControlBase 或 FooBase。

-奥辛

Because, quite frankly, the accepted pattern for naming abstract classes is already set ;-) It's with a "Base" suffix, like MyControlBase, or FooBase.

-Oisin

平定天下 2024-07-23 17:04:24

在Java中,许多“抽象”类都以“Abstract”为前缀,例如AbstractList等。

到底为什么人们需要仅通过读取类的名称来了解一个类是否是抽象的很重要。 在类名变得很长之前,我们只能塞进这么多的细节。

我个人认为接口的“I”前缀也很丑陋。 我相信人们不应该尝试在类名中编码这些细节。 我相信,通过将实现细节与接口名称相结合,可以得出真正有意义但简短的名称。 一个完美的例子是 Java 的 Map、HashMap 等,它们都非常具有描述性和简洁性。

In Java many "abstract" classes are prefixed with "Abstract" eg - AbstractList etc.

In the end why does it matter that one needs to know whether a class is abstract just from reading it's name. There's only so much detail one can cram into a class name before they become quite long.

I personally find the "I" prefix thing for interfaces quite ugly as well. I believe one should not try and encode such details in a class name. I believe by combining implementation details with the interface name one comes up with truely meaningful yet short names. A perfect example is Java's Map, HashMap etc all very discriptive and concise.

☆獨立☆ 2024-07-23 17:04:24

I 前缀很难看,但不幸的是这是微软的惯例。
在理想的世界中,我们都应该针对接口进行编程。 具体类将有一个后缀 Impl 或其他东西来将它们与接口或抽象类区分开来(我们不在乎,因为我们的 IoC 容器将为我们处理它!)

例如 Book 将是一个接口或抽象类,但它确实不是'哪个都没关系。
大多数时候我们都会使用漂亮干净的名称进行编程。 实际使用的实现可以轻松地从配置文件插入。

但可惜的是,我们并不生活在这样一个理想的世界中。

The I prefix is ugly, but unfortunately that's the Microsoft convention.
In an ideal world, we should all be programming to interfaces. Concrete classes would have a suffix Impl or something to distinguish them from the interface or abstract class (and we don't care because our IoC containers will handle that for us!)

e.g. Book would be an interface or abstract class and it really doesn't matter which.
We would be programming to nice clean names most of the time. The actual implementation to use is easily pluggable from a configuration file.

But alas, we don't live in such an ideal world.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文