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

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

问题描述

我是比较新的C#,每次我开始对C#项目工作的时间(我只在C#中工作的近成熟的项目),我不知道为什么有没有内部类?

也许我不明白他们的目标。对我来说,内部类 - 至少私有内部类 - 看起来很像帕斯卡/ Modula-2的/ Ada的内部程序:他们可以打破较小的部分主类,以缓解理解<。 / p>

举例:这里是什么是看到大部分的时间:

 公共类ClassA的
{
   公共方法a()
   {
      &LT;有的code&GT;
      myObjectClassB.DoSomething(); // ClassB的仅用于ClassA的
      &LT;有的code&GT;
   }
}公共类ClassB的
{
   公共DoSomething的()
   {
   }
}

由于ClassB的将被用于(至少一段时间)只有ClassA的,我的猜测是,这code将pssed如下更好的前$ P $:

 公共类ClassA的
   {
      公共方法a()
      {
         &LT;有的code&GT;
         myObjectClassB.DoSomething(); // B类只有通过可用的ClassA
         &LT;有的code&GT;
      }      私有类ClassB的
      {
         公共DoSomething的()
         {
         }
      }
   }

我将很高兴听到你对这个问题 - 我是正确的。

在此先感谢!


解决方案

嵌套类(可能是最好的避免单词内部在C#中嵌套类是在Java内部类有所不同)确实是非常有用的。

还没有被提到的一个模式是更好枚举模式 - 其中可以甚至比在Java的更灵活的:

 公共抽象类MyCleverEnum
{
    公共静态只读MyCleverEnum首先=新FirstCleverEnum();
    公共静态只读MyCleverEnum秒=新SecondCleverEnum();    //只能通过这种类型的*和嵌套类型*被称为
    私人MyCleverEnum()
    {
    }    公共抽象无效的someMethod();
    公共抽象无效AnotherMethod();    私有类FirstCleverEnum:MyCleverEnum
    {
        公共覆盖无效的someMethod()
        {
             //此处特有的第一行为
        }        公共覆盖无效AnotherMethod()
        {
             //此处特有的第一行为
        }
    }    私有类SecondCleverEnum:MyCleverEnum
    {
        公共覆盖无效的someMethod()
        {
             //此处特有的第二行为
        }        公共覆盖无效AnotherMethod()
        {
             //此处特有的第二行为
        }
    }
}

我们可以用一些语言支持做自动做一些这一点 - 有很多的选择我还没有在这里显示,就像没有实际使用嵌套类的所有值,或使用相同的嵌套类多个值,但给他们不同的构造函数的参数。但基本上,一个事实,即嵌套类可以调用私有构造赋予很大的权力。

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?

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

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?

Thanks in advance!

解决方案

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.

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天全站免登陆