.NET 嵌套类

发布于 2024-07-11 21:14:03 字数 787 浏览 6 评论 0原文

我正在开发的当前类库将有一个基类(Field),其中包含超过 50 个特定的“字段”类型,这些类型将从“Field”继承并嵌套以保持可读性。 例如...

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

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

到目前为止,一切看起来都不错,我可以使用如下所示的代码:

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

但是,为什么这也有效? 这里发生了什么让编译器/IDE 智能感知继续链接这些“FieldA”?

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

无论如何,这并不是应用程序的破坏,但我认为它很奇怪。 在 Boo 中做同样的事情(这是库所使用的实际语言)。

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.");
        }
    }
}

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

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

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阿楠 2024-07-18 21:14:03

听起来你想要类似的东西:

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

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

否则你将定义一个带有内部类的基类,继承者也会得到它。 因此,当您从外部类继承来创建内部类时,您就开始了一个循环。

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.");
    }
}

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.

把昨日还给我 2024-07-18 21:14:03
  1. Field 有一个名为 FieldApublic 嵌套类,
  2. FieldA 继承自 Field
  3. 因此你始终可以从 FieldA 访问 FieldA

该引用不会创建无限链,它只是指向同一个类。 (一些测试代码

当您访问FieldA.FieldA时,后面的FieldA 是可访问的,因为前一个 FieldAField 的实例,因此后一个 FieldA 实际上访问 字段.FieldA

  1. Field has a public nested-class named FieldA
  2. FieldA inherits from Field
  3. thus you can always access FieldA from FieldA.

The reference isn't creating infinite chains, it is simply pointing to the same class. (some test code)

When you access FieldA.FieldA, the latter FieldA is accessible due to the fact that the former FieldA is an instance of Field so the latter FieldA actually access Field.FieldA

伏妖词 2024-07-18 21:14:03

FieldA 继承对类 FieldA 的引用,类 FieldA 继承类 FieldA 的引用,类 FieldA 继承类 FieldA 的引用,类 FieldA 继承类 FieldA 的引用,类 FieldA 继承类 FieldA 的引用,类 FieldA 继承类 FieldA 的引用,类 FieldA 继承类 FieldA 的引用,其中...

它之所以有效,是因为这就是你告诉它做的事情。

FieldA inherits a reference to class FieldA which inherits a reference to class FieldA which inherits a reference to class FieldA which inherits a reference to class FieldA which inherits a reference to class FieldA which inherits a reference to class FieldA which inherits a reference to class FieldA which...

It works because that's what you told it to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文