我不确定如何提出这个问题,但基本上我想要做的是从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);
}
从我的静态虚空主,我希望能够调用dog.Speak(),但我不能,因为它没有在IDog中实现。我知道我可以从派生类中轻松访问Speak()但是可以从实现中访问它还是设计不好?
答案 0 :(得分:6)
假设所有IDog
都是IAnimal
,请将IDog
声明为实施IAnimal
public interface IDog : IAnimal