.NET 嵌套类
我正在开发的当前类库将有一个基类(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你想要类似的东西:
否则你将定义一个带有内部类的基类,继承者也会得到它。 因此,当您从外部类继承来创建内部类时,您就开始了一个循环。
Sounds like you wanted something like:
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.
Field
有一个名为FieldA
的 public 嵌套类,FieldA
继承自Field
FieldA
访问FieldA
。该引用不会创建无限链,它只是指向同一个类。 (一些测试代码)
当您访问
FieldA.FieldA
时,后面的FieldA
是可访问的,因为前一个FieldA
是Field
的实例,因此后一个FieldA
实际上访问字段.FieldA
Field
has a public nested-class namedFieldA
FieldA
inherits fromField
FieldA
fromFieldA
.The reference isn't creating infinite chains, it is simply pointing to the same class. (some test code)
When you access
FieldA.FieldA
, the latterFieldA
is accessible due to the fact that the formerFieldA
is an instance ofField
so the latterFieldA
actually accessField.FieldA
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.