如何动态创建类对象?
假设我有一个类盒子,并且用户可以创建盒子。怎么做呢?我知道我通过 className objectName(args);
创建对象,但是如何根据用户输入动态地创建对象?
Let's say I have a class box, and a user can create boxes. How to do it? I understand I create objects by className objectName(args);
but how to do it dynamically, depending on the user input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正确的答案取决于您要创建实例的不同类的数量。
如果数量很大(应用程序应该能够创建应用程序中任何类的实例),则应该使用.Net 的反射功能。但是,说实话,我不太喜欢在业务逻辑中使用反射,所以我建议不要这样做。
我认为实际上您想要为其创建实例的类数量有限。所有其他答案都做出了这个假设。你真正需要的是工厂模式。在接下来的代码中,我还假设您要创建实例的类都派生自同一个基类,比方说 Animal,如下所示:
然后创建一个抽象工厂,它是创建动物的接口:
然后创建子类对于每种不同种类的动物。例如,对于 Dog 类,这将变成这样:
对于猫来说也是如此。
DogFactory::create 方法会否决 IFactory::create 方法,即使它们的返回类型不同。这就是所谓的协变返回类型。只要子类方法的返回类型是基类返回类型的子类,这是允许的。
现在你可以做的就是将所有这些工厂的实例放在一个映射中,如下所示:
用户输入后,你必须找到正确的工厂,并要求它创建动物的实例:
这是典型的抽象工厂方法。
还有其他方法。在自学 C++ 时,我写了一篇关于它的 CodeProject 小文章。您可以在这里找到它: http://www.codeproject.com/KB/architecture/all_kinds_of_factories .aspx。
祝你好运。
The correct answer depends on the number of different classes of which you want to create the instances.
If the number is huge (the application should be able to create an instance of any class in your application), you should use the reflection functionality of .Net. But, to be honest, I'm not a big fan of using reflection in business logic, so I would advise not to do this.
I think that in reality you have a limited number on classes for which you want to create instances. And all the other answers make this assumption. What you actually need is a factory pattern. In the next code I also assume that the classes of which you want to create instances, all derive from the same base class, let's say Animal, like this:
Then create an abstract factory which is an interface that creates an animal:
Then create subclasses for each of the different kinds of animals. E.g. for the Dog class this will become this:
And the same for the cat.
The DogFactory::create method overrules the IFactory::create method, even if their return type is different. This is what is called co-variant return types. This is allowed as long as the return type of the subclass's method is a subclass of the return type of the base class.
What you can now do is put instances of all these factories in a map, like this:
After the user input, you have to find the correct factory, and ask it to create the instance of the animal:
This is the typical abstract factory approach.
There are other approaches as well. When teaching myself C++ I wrote a small CodeProject article about it. You can find it here: http://www.codeproject.com/KB/architecture/all_kinds_of_factories.aspx.
Good luck.
以下工厂方法根据用户输入动态创建
Box
实例:当然,
PrettyBigBox
和SmallBox
都派生自Box
>。查看C++ 设计模式维基百科,因为其中之一可能适用于您的问题。The following factory method creates
Box
instances dynamically based on user input:Of course,
PrettyBigBox
andSmallBox
both derive fromBox
. Have a look at the creational patterns in the C++ design patterns wikibook, as one of them probably applies to your problem.在 C++ 中,可以使用自动(堆栈)和动态(堆)存储来分配对象。
您可以使用指针和堆分配来动态构造对象,如下所示:
相关:“工厂”面向对象的设计模式
In C++, it is possible to allocate objects using automatic (stack) and dynamic (heap) storage.
You can use pointers and heap-allocation to dynamically construct objects as in:
Related: the "Factory" object-oriented design pattern