.NET Nested Classes

自古美人都是妖i 提交于 2019-12-02 05:32:47

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.

  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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!