.NET嵌套类 [英] .NET Nested Classes

查看:215
本文介绍了.NET嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前的类库我的工作将有一个基类(场)有50具体的场,将继承场和嵌套为保持可读性类型。例如...

The current class library I am working on will have a base class (Field) with over 50 specific "field" types which will inherit from "Field" and nested for maintain readability. For example...

abstract class Field
{
    public int Length { get; set; }

    public class FieldA : Field
    {
        public static void DoSomething()
        {
            Console.WriteLine("Did something.");
        }
    }
}

到目前为止,一切都看起来不错,我可以用如图所示的code:

So far everything looks good and I can use the code as shown:

class Program
{
    static void Main(string[] args)
    {
        Field.FieldA.DoSomething();
    }
}

但是,为什么这项工作呢?这是怎么回事就在这里,可以让编译器/ IDE智能感知继续链这些FIELDA的?

However, why does this work as well? What is going on here that allows the compiler / IDE intellisense to continue to chain these "FieldA"'s?

class Program
{
    static void Main(string[] args)
    {
        Field.FieldA.FieldA.FieldA.FieldA.FieldA.FieldA.FieldA.DoSomething();
    }
}

这不是断申请以任何方式,但认为它是独特的。难道(正在使用的库,它是实际的语言)的嘘了同样的事情。

It's not application breaking by any means, but thought it was peculiar. Does the same thing in Boo (which is the actual language being used for the library).

推荐答案

听起来像是你想是这样的:

Sounds like you wanted something like:

abstract class Field
{
    public int Length { get; set; }
}

public class FieldA : Field
{
    public static void DoSomething()
    {
        Console.WriteLine("Did something.");
    }
}

否则你定义一个基类有一个内部类的吧,这inheritorrs也将获得。所以,当你从外部类继承,使内部类,你开始一个循环。

Otherwise you're defining a base class with an inner class in it, which inheritorrs will also get. So when you inherit from the outer class to make the inner class, you're starting a loop.

这篇关于.NET嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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