如果一个对象继承自一个继承自接口的抽象类,那么该对象是否会继承该接口?

发布于 2024-12-05 04:48:50 字数 89 浏览 4 评论 0原文

如果一个对象继承自一个抽象类,而该抽象类又继承了一个接口,那么该对象是否会继承该接口?

该接口可以与依赖注入容器一起使用吗?

谢谢。

If an object inherit from an abstract class that inherite from an interface will the object inherit from the interface ?

Will this interface can be used with Dependency Injection Container ?

Thanks.

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

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

发布评论

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

评论(2

ι不睡觉的鱼゛ 2024-12-12 04:48:50

是的,它确实实现了该接口,并且因为这是真的,所以它可以作为依赖项注入。

这是一个证明接口实现的快速测试:

using System;

interface IParent {}
abstract class Parent : IParent {}

class Example : Parent
{
    static void Main()
    {
        Console.WriteLine(new Example() is IParent);
    }
}

Yes it does implement that interface and since this is true it can be injected as a dependency.

Here is a quick test that proves the interface implementation:

using System;

interface IParent {}
abstract class Parent : IParent {}

class Example : Parent
{
    static void Main()
    {
        Console.WriteLine(new Example() is IParent);
    }
}
浮生未歇 2024-12-12 04:48:50

我认为接口是一种契约。由于抽象类继承自接口,因此必然需要实现接口的功能。并且要求对象(实际上是它的类)重写抽象类的抽象函数。
在我看来,这种模式只适合稳定的接口或抽象类。因为一旦接口或抽象类的契约发生变化,那将是一场灾难。哎呀~~,你搞砸了构建!

我更喜欢使用虚拟类作为基类并使函数虚拟,这样您就可以像接口/抽象类解决方案一样在子类中重写它们,并且也可以轻松添加新的重载。

I consider interface as a kind of contract. Since the abstract class inherite from the interface, it is surely demanded to implement the functions of the interface. And the object (in fact the class of it) is demanded to override the abstract function of the abstract class.
In my opinion, this pattern is only suitable for stable interface or abstract class. Cause once there was change to the contract of the interface or abstract class, that would be a disaster. Oops~~, you screw the build!

I prefer use the virtual class for base class and make the functions virtual, so you can override them in the subclasses as same as the interface/abstractclass solution, and new overloads can be added easily as well.

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