如果abstact类实现了接口,那么是否需要在抽象类中实现接口方法。 [英] If abstact class implements the interface then is there any need to implement interface method in abstract class.

查看:121
本文介绍了如果abstact类实现了接口,那么是否需要在抽象类中实现接口方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有方法add()的Iadd()接口。我有抽象类math:Iadd(),我还有一个派生类derived(),它继承了抽象类。在哪里我写了add()方法实施?在abstratc课程或派生课程中?请帮助我。



Suppose i have Iadd() interface with method add().I have abstract class math:Iadd() and also i have one derived class derived() which inherites the abstract class.where shoud i write the add() method implementation ? In abstratc class or in derived class ?Please help me on this.

Iadd()
{
add()
}

abstratc class add():Iadd()
{
//where i can implement the Add() method and how ?
}

class derived:add()
{
//where i can implement the Add() method and how?

}

推荐答案

否,您不能在其继承列表中具有此接口的任何类中跳过接口方法实现。这个类是否抽象并不重要。如果你想到这一点,或许你可以理解为什么它是这样设计的;简而言之,它会破坏接口的目的。



同时,您可以创建接口方法的实现,由抽象虚方法表示。这是一个奇怪的事情:从接口的角度来看,这是它的方法的实现,但是方法是抽象的,所以没有实现。无论如何,了解它是如何工作并理解这是非常合理的事情更为重要。



它有合理的用途,你可以稍后覆盖这个方法。实际上,例如,如果您可以更早地实现接口方法的一部分,稍后部分实现,则可能是有意义的。它的外观如下:

No, you cannot skip interface method implementation in any class which has this interface in its inheritance list. It does not matter if this class is abstract or not. If you think at this, perhaps you can understand why it is designed this way; in brief, it would defeat the purpose of interfaces.

At the same time, you can create an implementation of the interface method, represented by an abstract virtual method. This is a weird thing: from the standpoint of interface, this is an implementation of its method, but the method is abstract, so there is no an implementation. Anyway, it's more important to understand how it works and understand that this is quite a reasonable thing.

It would have reasonable purpose, you can later override the method. Practically, it may make sense if you, for example, can implement part of interface methods earlier, and part later. This is how it may look:
interface ISample {
    void First
    void Second();
}

abstract class Base : ISample {
    public void First() {
        // do something,
        // or not, then such method
        // is sometimes called 'pseudo-abstract'
    }
    public abstract void Second(); // virtual abstract 
}



或者你可以使用通常更好的显式实现,因为它只允许通过接口引用调用接口成员而不是通过实现类实例:


Or you can use explicit implementation which is often better, because it would allow to call interface members only through an interface reference and not through the implementing class instance:

abstract class Base : ISample {
    void ISample.First() { }
    abstract void ISample.Second(); 
}



然后你可以在一个派生类中完全实现 ISample.Second ,并且最后得到一些可以实例化的非抽象类。然后,您可以将类实例分配给接口类型的变量并调用接口方法/属性。



-SA


And then you can later fully implement ISample.Second in one of derived classes, and finally get some non-abstract classes which could be instantiated. Then you can assign the class instance to a variable of an interface type and call interface methods/properties.

—SA


这篇关于如果abstact类实现了接口,那么是否需要在抽象类中实现接口方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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