[C#]有什么意义,嵌套类的用途是什么? [英] [C#] what's the point and what's the use of nested classes ?

查看:93
本文介绍了[C#]有什么意义,嵌套类的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我想问一下这里的人们使用嵌套类有什么意义?



我尝试过的事情:



在CodeProject.com上提出问题

Hi. I wanna ask you people here what is the point of using nested classes ?

What I have tried:

Asking a question here in CodeProject.com

推荐答案

在这里问这些问题有什么意义?

去拿一本好书 - 或者更好的课程并开始学习......
What's the point to ask all these questions here?
Go and fetch a good book - or better a course and start learning...


他们可以帮助提供隐藏课程:

They can help provide "hidden" classes:
public class MyClass
   {
   private class MyNestedClass
      {
      public MyNestedClass()
         {
         }
      }
    public void AMethod()
        {
        MyNestedClass mnc = new MyNestedClass();
        }
   }

只有MyClass可以创建MyNestedClass的实例 - 或者确实看到它 - 外面的类甚至不知道它在那里。



它们还可以帮助将可能具有共同名称的类分组到包含类的伞中。这样,你必须使用完全限定的类名来使用它,它不会与任何其他类名冲突:

Only MyClass can create instances of MyNestedClass - or indeed see it - classes outside don't even know it's there.

They can also help to "group" classes which might have common names together into the "umbrella" of the containing class. That way, you have to use the fully qualified class name to use it and it doesn't conflict with any other class name:

    public void MyMethod()
        {
        MyClass.MyNestedClass mnc = new MyClass.MyNestedClass();
        }
    }
public class MyClass
    {
    public class MyNestedClass
        {
        public MyNestedClass()
            {
            }
        }
    }


这篇关于[C#]有什么意义,嵌套类的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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