使用虚拟构造函数? [英] Where virtual constructors are used?

查看:212
本文介绍了使用虚拟构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过关于虚拟构造函数用于实现一些设计模式,但没有理解任何虚拟构造函数的需要。那么什么是虚拟构造函数,以及为什么我们真正需要它们?

解决方案

在大多数编程语言中,afaik不能找到虚拟构造函数。在构建对象之后,在运行时评估虚拟成员的覆盖,但在大多数语言中,在构建实例时,您需要知道实际的类。因此,虚拟构造函数在这些语言中没有意义。



在.NET中,您可以通过反射获得类似的解决方案,也就是说,您可以通过类型的实例表示要构造的对象的类。对于泛型支持,你也可以实现类似的东西,但它不是虚拟构造函数。



我使用的唯一的编程语言,有真正的虚拟构造函数是Delphi。在Delphi中,有一个特定的元类类型,即表示元类的特定编程结构(而在.NET中,元类,Type类只是一个普通类的实例)。所以如果你有一个类叫TMyClass - Deplhi命名约定;)

  TMyClass:Class ... 

您可以像下面这样声明元类:

  TMyMetaClass:TMyClass类

现在,您可以声明一个变量TMyMetaClass类型,

  MyMetaClassVariable:TMyMetaClass 
...
//分配元类来引用我们的具体class
MyMetaClassVariable:= TMyClass;

您可以通过此变量构建一个新实例

  MyClassInstance:= MyMetaClassVariable.Create(); 

现在,MyMetaClassVariable可以引用任何一个类,它是一个专门化的TMyClass。如果构造函数被声明为virtual,那么这个变量将被构造成具有该特定类的实例。



同样,你可以在Delphi中声明虚拟静态方法,并通过元类的实例调用它们。



那么另一个问题?为什么我们需要他们?好吧,在Delphi中,他们解决了一些与.NET中的Type类相同的问题,允许你构造对象,在设计时你不知道类名。
例如,当你设计一个表单并放入一堆控件时,这些数据必须由设计器序列化,并反序列化。当表单反序列化时,它实际上是被读取的元类型,并且通过调用元类型上的虚拟构造函数构造正确的实例(是TextBox,ComboBox等)。


I read about virtual constructors are used for implementing some design patterns, but didn't understood any need of virtual constructors. So what are virtual constructors and why we really need them?

解决方案

In most programming languages, afaik, you cannot find virtual constructors. Which override of a virtual members are evaluated at runtime after an object is constructed, but in most languages you need to know the actual class when constructing the instance. Therefore virtual constructors make no sense in these languages.

In .NET, you can get a similar solution through reflection, i.e. you can construct an object through an instance of the Type class that represents the object you want to construct. And with generic support, you can also achieve something similar, but it's not virtual constructors.

The only programming language that I have worked with that have true virtual constructors is Delphi. In Delphi, there is a specific "Metaclass type", i.e. a specific programming construct that represents a metaclass (whereas in .NET, the meta class, the Type class, is just an instance of a normal class). So if you have a class called TMyClass - Deplhi naming conventions ;)

TMyClass : Class ...

You can declare the metaclass like this

TMyMetaClass : class of TMyClass

Now, you can declare a variable that is of TMyMetaClass type,

MyMetaClassVariable : TMyMetaClass
... 
// Assign the meta class to refer to our concrete class
MyMetaClassVariable := TMyClass;

And you can construct a new instance through this variable

MyClassInstance := MyMetaClassVariable.Create();

Now, the MyMetaClassVariable can refer to any class that is either TMyClass of a specialization hereof. If the constructor is declared virtual, then the variable will be constructed with an instance of that specific class.

In the same way, you can declare virtual static methods in Delphi, and call them through an instance of the metaclass.

So the other question? Why do we need them? Well, in Delphi, they solve some of the same problems as the Type class in .NET, allowing you to construct objects where you don't know the class name at design time. For example, when you design a form and you put in a bunch of controls, this data has to be serialized by the designer, and deserialized. When the form is deserialized, then it is actually the metatypes that are read, and the correct instances (be it TextBox, ComboBox, etc) are constructed by calling the virtual constructor on the metatype.

这篇关于使用虚拟构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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