构造函数可以返回子类吗?

发布于 2024-08-23 15:19:34 字数 241 浏览 3 评论 0原文

给出以下客户端代码:

var obj = new Class1();

是否有任何方法可以修改 Class1 的构造函数,以便它实际上返回一个子类(或其他一些替代实现)?

我希望 obj 根据某些条件获得两种不同实现之一。显然,我可以改为使用工厂或 DI 框架,但如果可能的话,我想避免更改客户端代码。

我认为答案是否定的,但我想知道是否有一些聪明的方法可以实现这一点。

Given the following client code:

var obj = new Class1();

Is there any way to modify the constructor of Class1 so that it will actually return a subclass (or some other alternate implementation) instead?

I would like obj to get one of two different implementations, depending on some condition. Obviously, I could change to using a factory or DI framework, but I'd like to avoid changing the client code, if possible.

I assume the answer is no, but I wonder if there's some clever way of making that happen.

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

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

发布评论

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

评论(4

三生殊途 2024-08-30 15:19:35

对于此类事情,您需要查看工厂模式。
此外,根据您的需求,我建议您查看减少耦合的通用方法。对类的构造函数的调用是程序中最难的耦合,这使得交换实现之类的事情变得很麻烦,正如您自己发现的那样。

阅读“控制反转”和“依赖注入”,也许这就是您真正想要的。

可以在此处找到一个不错的库。

For things like these you want to check out the Factory pattern.
In addition, depending on your needs, I'd recommend looking at general methods to reduce coupling. Your call to the constructor of the class is the hardest coupling you can have in a program and makes things like swapping the implementation out a hassle, as you found out yourself.

Read about "Inversion of control" and "Dependency Injection", maybe that's what you really look for.

A nice library can be found here.

狠疯拽 2024-08-30 15:19:35

使用注入库。下面的链接提供了一个很棒的注入库列表以及它们的性能比较

http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison

但我建议,性能很少是您选择的标准如果启用了注入库,大多数应用程序都不会实例化这个人在测试中使用的对象数量。我正在使用 NInject ,它可以很好地完成工作,但是如果我要开始另一个项目,我可能会给出 simpleinjector 试了一下,它似乎拥有我在 NInject 中使用的所有功能,并且在性能比较中表现良好。

Use an injection library. The link below presents a great list of injection libraries out there and comparison of their performance

http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison

I would suggest however that performance is rarely a criteria you would choose an injection library on, most applications wouldn't instantiate the number of objects this guy has used in his tests. I'm using NInject which does the job well, however if I were to start another project I'd probably give simpleinjector a go, it seems to have all the features I use in NInject and it does well in the performance comparisons.

时光无声 2024-08-30 15:19:34

您可以用工厂方法替换构造函数,并根据参数返回您喜欢的任何内容:

public Class2 : Class1 {}

public static Class1 CreateClass1(bool returnDerivedClass)
{
    if (returnDerivedClass)
    {
        return new Class2();
    }
    else
    {
        return new Class1();
    }
}

You can replace the constructor with a factory method, and return whatever you like, depending on the parameters:

public Class2 : Class1 {}

public static Class1 CreateClass1(bool returnDerivedClass)
{
    if (returnDerivedClass)
    {
        return new Class2();
    }
    else
    {
        return new Class1();
    }
}
骑趴 2024-08-30 15:19:34

这是不可能的。

聪明的解决方法包括用静态函数替换构造函数或(不推荐)使用基类的包装器,以及创建不同的包装类。

This is not possible.

Clever workarounds include replacing the constructor with a static function or (not recommended) using a wrapper around the base class, and creating different wrapped classes.

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