Why can't I override my interface its methods?

蓝咒 提交于 2020-05-13 19:24:07

问题


Let's say I have an interface as follows.

interface CardHolder : IEnumerable<Card>
{
    /// <summary> ...
    void PutCard(Card card);

    /// <summary> ...
    void PutCards(Card[] card);

    /// Some more methods...
}

I implement it as follows.

public class ArrayCardHolder : CardHolder
{
    private Card[] _cards;
    private int _size = 0;

    public ArrayCardHolder(int capacity)
    {
        _cards = new Card[capacity];
    }

    public void PutCard(Card card)
    {
        if (IsFull())
            throw new Exception("This CardHolder is full. Capacity: " + Capacity());

        _cards[_size++] = card;
    }

    public void PutCards(Card[] cards)
    {
        if (_size + cards.Length > _cards.Length)
            throw new Exception("Adding the Cards would exceed this CardHolder its capacity. Capacity: " + Capacity());

        for (int index = 0; index < cards.Length; index++)
            _cards[_size++] = cards[index];
    }
    public IEnumerator<Card> GetEnumerator()
    {
        for (int index = 0; index < _size; index++)
            yield return _cards[index];
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

    ///More methods.

}

Why can I not use the override keyword in my ArrayCardHolder (e.g. public void override PutCard(Card card) { ///implementation } to indicate that the method implements (i.e. overrides) the interface? In that case, the project will refuse to build.

Why does it work however when overriding ToString()? And why doesn't it work when implementing CompareTo(T t) from IComparable<T>?

What should I use instead? I'm worried that the documentation from the interface will not apply to my implementing methods. Such is the case in Java when the @Override annotation is used.


回答1:


Interface's methods are not overriden, they are implemented. You are confused with abstract/virtual methods which can be overriden.

Example:

public interface IFoo    
{
    void DoA();
}

public abstract class BaseFoo : IFoo
{
    public void DoA() { } // *this HAS to be implemented*
    public virtual void DoB() { } 
}

public abstract class MyFoo : BaseFoo
{
    // *this CAN be implemented, which would override the default implementation*
    public override void DoB() { } 
}

As others metioned, ToString is a virtual method of the base class object, that is why you can override it.




回答2:


You can override any method of base type if it is marked as virtual. For example you can override ToString() method as it is marked virtual in object class. And object is the base type in .Net.

 public override string ToString()
    {
        return base.ToString();
    }

Interfaces are implemented because they done have any implementation, hence there is nothing to override. For Example IComparable is an interface with CompateTo() method, It has no implementation, Hence you implement it in the class inheriting this interface.

public int CompareTo(object obj)
    {
        throw new NotImplementedException();
    }

I hope I made it clear.




回答3:


An "interface" is a description of "what the public-facing face of some particular programming-thing must look like."

Any "concrete implementation" of that interface must do (at least) everything that the interface called for. (How it "gets 'er done" is entirely up to the implementor.)

So now, let's say that you've got two classes, Dad and Teenager, both of whom implement the same interface. But Teenager (although obviously a descendent of Dad) wants to do one of those things ... say, play_music ... a little bit differently. Teenager can override Dad's method.

So long as Teenager's implementation continues to conform to the strictures of the interface, our teen can play his music just as loudly as he likes.




回答4:


You are not overriding methods, you are implementing members of an interface.

In C#, override is only used when you are overriding an existing implementation. A case of this is ToString(). The method you are overriding must be marked virtual on the base class.




回答5:


.ToString is a virtual method in a base class Object . That's why you can override it. But you don't override an interface method, you implement it.

If you want to provide a default implementation and maybe override it in a class, then you need to use a base class with a virtual method. So :

Interface method: You must provide implementation in the class.

Abstract method: You must provide implementation in the derived class.

virtual method: you may provide implementation in the derived class or keep the default implementation. (or mix both by calling base.MethodName(); then providing additional code)

Example:

public interface IContract
{
    // It is a must to implement this method in classes
    void MustImplement();
}

public abstract class BaseClass
{
    // Just signature, No implementation. It is a must to implement.
    public abstract void IAmAbstract();

    public virtual void IAmVirtual()
    {
        Console.WriteLine("I provide default implementation");
    }
}

public class DerivedClass : BaseClass, IContract
{
    public override void IAmAbstract()
    {
        Console.WriteLine("provides Abstract Method implementation In Derived Class");
    }

    // It is optional to override this method
    public override void IAmVirtual()
    {
        // class default implementation
        base.IAmVirtual();

        Console.WriteLine("provides Additional virtual Method implementation In Derived Class");
    }

    public void MustImplement()
    {
        Console.WriteLine("provides Interface Method implementation In Derived Class");
    }
}


来源:https://stackoverflow.com/questions/38273352/why-cant-i-override-my-interface-its-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!