C# 中的私有内部类 - 为什么它们不经常使用? [英] Private inner classes in C# - why aren't they used more often?

查看:27
本文介绍了C# 中的私有内部类 - 为什么它们不经常使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C# 比较陌生,每次开始从事 C# 项目时(我只从事过近乎成熟的 C# 项目),我想知道为什么没有内部类?

I am relatively new to C# and each time I begin to work on a C# project (I only worked on nearly mature projects in C#) I wonder why there are no inner classes?

也许我不明白他们的目标.对我来说,内部类——至少是私有内部类——看起来很像 Pascal/Modula-2/Ada 中的内部过程":它们允许将主类分解成更小的部分,以便于理解.

Maybe I don't understand their goal. To me, inner classes -- at least private inner classes -- look a lot like "inner procedures" in Pascal / Modula-2 / Ada : they allow to break down a main class in smaller parts in order to ease the understanding.

示例:这是大多数时候看到的内容:

Example : here is what is see most of the time :

public class ClassA
{
   public MethodA()
   {
      <some code>
      myObjectClassB.DoSomething(); // ClassB is only used by ClassA
      <some code>
   }
}

public class ClassB
{
   public DoSomething()
   {
   }
}

由于 ClassB 将仅由 ClassA 使用(至少有一段时间),我猜这段代码会更好地表达如下:

Since ClassB will be used (at least for a while) only by ClassA, my guess is that this code would be better expressed as follow :

   public class ClassA
   {
      public MethodA()
      {
         <some code>
         myObjectClassB.DoSomething(); // Class B is only usable by ClassA
         <some code>
      }

      private class ClassB
      {
         public DoSomething()
         {
         }
      }
   }

我很高兴收到您关于这个主题的消息 - 我说得对吗?

I would be glad to hear from you on this subject - Am I right?

推荐答案

嵌套类(可能最好避免使用内部"这个词,因为 C# 中的嵌套类与 Java 中的内部类有些不同)确实非常有用.

Nested classes (probably best to avoid the word "inner" as nested classes in C# are somewhat different to inner classes in Java) can indeed be very useful.

一种没有提到的模式是更好的枚举"模式——它比 Java 中的更灵活:

One pattern which hasn't been mentioned is the "better enum" pattern - which can be even more flexible than the one in Java:

public abstract class MyCleverEnum
{
    public static readonly MyCleverEnum First = new FirstCleverEnum();
    public static readonly MyCleverEnum Second = new SecondCleverEnum();

    // Can only be called by this type *and nested types*
    private MyCleverEnum()
    {
    }

    public abstract void SomeMethod();
    public abstract void AnotherMethod();

    private class FirstCleverEnum : MyCleverEnum
    {
        public override void SomeMethod()
        {
             // First-specific behaviour here
        }

        public override void AnotherMethod()
        {
             // First-specific behaviour here
        }
    }

    private class SecondCleverEnum : MyCleverEnum
    {
        public override void SomeMethod()
        {
             // Second-specific behaviour here
        }

        public override void AnotherMethod()
        {
             // Second-specific behaviour here
        }
    }
}

我们可以使用一些语言支持来自动完成其中的一些工作 - 还有很多我没有在这里展示的选项,比如实际上不对所有值使用嵌套类,或者对所有值使用相同的嵌套类多个值,但给它们不同的构造函数参数.但基本上,嵌套类可以调用私有构造函数这一事实提供了很多功能.

We could do with some language support to do some of this automatically - and there are lots of options I haven't shown here, like not actually using a nested class for all of the values, or using the same nested class for multiple values, but giving them different constructor parameters. But basically, the fact that the nested class can call the private constructor gives a lot of power.

这篇关于C# 中的私有内部类 - 为什么它们不经常使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆