接口 - 实现抽象类方法

Ano*_*ous 1 c# oop

我不确定如何提出这个问题,但基本上我想要做的是从IDog访问抽象方法.考虑一下:

    static void Main(string[] args)
    {
        IDog dog = new Dog();
        dog.CatchFrisbee();
        dog.Speak("Bark") // error -> no implementation
        Console.ReadLine();
    }

    public class Dog : Animal, IDog
    {
        public void CatchFrisbee()
        {
            Console.WriteLine("Dog Catching Frisbee");
        }
    }
    public abstract class Animal : IAnimal
    {
        public void Speak(string sayWhat)
        {
            Console.WriteLine(sayWhat);
        }

        public void Die()
        {
            Console.WriteLine("No Longer Exists");
        }
    }
    public interface IDog
    {
        void CatchFrisbee();
    }
    public interface IAnimal
    {
        void Die();

        void Speak(string sayWhat);
    }
Run Code Online (Sandbox Code Playgroud)

从我的静态虚空主,我希望能够调用dog.Speak(),但我不能,因为它没有在IDog中实现.我知道我可以从派生类中轻松访问Speak(),但是可以从实现中访问它还是设计不好?

spe*_*der 6

假设所有IDogs都是IAnimals,则声明IDog为实现IAnimal

public interface IDog : IAnimal
Run Code Online (Sandbox Code Playgroud)