C# - 定义公共嵌套类:我们应该这样做吗?
过去我避免定义嵌套类。但是,如果一个类只在主类中使用,那么嵌套类是它的理想位置吗?关于这个主题是否有任何公认的惯例?
public class PrimaryClass
{
public class SubClass
{
// ...
}
public SubClass MySubClass { get; set; }
// ...
}
或者应该总是这样
public class SubClass
{
// ...
}
public class PrimaryClass
{
public SubClass MySubClass { get; set; }
// ...
}
In the past I have avoided defining nested classes. But if a class is only ever going to be used inside the primary-class, then is nesting the class the ideal place for it? Are there any accepted conventions on this subject?
public class PrimaryClass
{
public class SubClass
{
// ...
}
public SubClass MySubClass { get; set; }
// ...
}
Or should it always be
public class SubClass
{
// ...
}
public class PrimaryClass
{
public SubClass MySubClass { get; set; }
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是完全正确的——如果子类只在主类中使用过,那么最好将关注点分开。在 Java 中,这是一种称为匿名内部类的语言级构造。如果需要,您可以稍后重构子类。
比较 C# 和 Java 的 wiki 文章有非常好的分析,涵盖了一般准则和特定于语言的问题。 http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java#Inner_and_local_classes
You are exactly correct- if the sub class is only ever used in the main class, it is good practice to separate the concerns. In Java, this is a language level construct called anonymous inner classes. You can always refactor the subclass out later if you need to.
The wiki article comparing C# and Java has a pretty good analysis, covering both the general guidelines and the language specific concerns. http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java#Inner_and_local_classes
如果内部类是其父外部类的内在部分,那么这样做就完全没问题。我看到内部类就像外部类的任何其他成员一样,除了它具有自己的一组方法和属性。
例如,发动机是机动车辆的固有部分。它不能在机动车之外发挥作用(因此毫无意义),所以换句话说,它存在的意义完全取决于机动车。当然,我在这里假设发动机除了机动车辆之外不能存在于其他任何东西中(抱歉,想不出更好的例子)。
在 C# 中,您当然也可以将引擎实现为单独的类,并从机动车辆类中引用它。这确实是您的选择。
If the inner class is an intrinsic part of its parent outer class, then it's perfectly fine to do so. I see an inner class just like any other member of the outer class, except it comes with its own set of methods and properties.
For example, an engine is an intrinsic part of a motor vehicle. It cannot function outside one (hence meaningless), so in other words, the meaningfulness of its existence depends solely on the motor vehicle. I am of course assuming here that an engine cannot exist in anything else but a motor vehicle (can't think of a better example, sorry).
in C#, you can of course also implement the engine as a separate class and refer to it from within the motor vehicle class. It's really a choice on your part.
除了仅在外部类内部使用嵌套类之外,在某些情况下您还希望将其实例传递给外部世界。通常您使用接口来做到这一点。下面是如何使用嵌套类和接口的示例:
DoSomething
的实现对外界完全隐藏。In addition to using the nested class solely inside the outer class, there are cases where you want to hand instances of it to the outside world. Typically you use inerfaces to do that. Here's an example of how you might use nested classes and an interface:
The implementation of
DoSomething
is completely hidden from the outside world.我倾向于将嵌套类仅定义为私有类,而不是公共类。这样,嵌套类将仅由封闭类使用。仅当嵌套类被多个顶级类使用时,我才会将该类移动到顶级。然后我通常将其内部化。
使用公共嵌套类应保留用于枚举。
I tend to define nested classes only as private, never public. The nested class, in this way, will be used only by the enclosing class. Only when the nested class is used by more than one top-level class do I move the class to top level. Then I usually make it internal.
Using public nested classes should be reserved for enumerations.