努力理解接口和抽象基类 [英] Struggling to understand interfaces and abstract base classes
问题描述
尽管我的头脑受到了我读过的东西的伤害,但我很难明白以下内容有什么问题:
Despite my head hurting with the stuff I have read, I am struggling to understand specifically what is wrong with the following:
public interface IAnimal
{
int Age();
int NumLegs();
}
public interface IBird : IAnimal
{
bool CanFly();
}
public interface IMammal : IAnimal
{
int RunningSpeed();
}
public abstract class AnimalBase : IAnimal
{
public abstract int Age();
public abstract void PrintAge();
public override int NumLegs() { return 4; } // Error!
};
public class Sparrow : AnimalBase, IBird
{
public override void PrintAge() { Console.WriteLine( $"Sparrow is {Age()} years old." ); }
public override bool CanFly() { return true; } // Error!
public override int Age() { return 3; }
}
我有几个错误(到目前为止):
在我的班级 AnimalBase
中,我收到错误消息'AnimalBase.NumLegs() - 找不到合适的方法来覆盖'。为什么? AnimalBase
实现 IAnimal
那么为什么它不能在界面中看到方法声明呢?
为什么 AnimalBase
中的抽象 Age()
好吗?我在这里猜测我只是声明一个与界面中的声明无关的全新方法,但是我不应该得到某种警告,我有点隐藏界面声明吗? />
第二个错误是完全相同的'Sparrow.CanFly() - 找不到合适的方法来覆盖'。为什么呢? Sparrow
实现 IBird
,为什么它不能看到 IBird.CanFly()
?
任何帮助理解这可能会节省我剩余的头发。
我尝试了什么:
我已经阅读并阅读了,我发现了很多可行的例子,但没有解释为什么我是试图做的显然是错的。
I have a couple of errors (so far):
In my class AnimalBase
, I get an error saying 'AnimalBase.NumLegs() - No suitable method found to override'. Why? AnimalBase
implements IAnimal
so why can't it see the method declaration in the interface?
And why is the abstract Age()
in AnimalBase
okay? I am guessing here that I am simply declaring a brand new method that has nothing to do with the declaration in the interface, but shouldn't I get some kind of warning that I am kind-of 'hiding' the interface declaration?
The second error is exactly the same 'Sparrow.CanFly() - No suitable method found to override'. Again why? Sparrow
implements IBird
, so why can't it see IBird.CanFly()
?
Any help understanding this might save my remaining hair.
What I have tried:
I have read and read, and I have found lots of examples that do work but no explanation of why what I am trying to do is apparently wrong.
推荐答案
麻雀是{Age()}岁。); }
public 覆盖 bool CanFly(){返回 true ; } // 错误!
public 覆盖 int 年龄(){返回 3 ; }
}
"Sparrow is {Age()} years old." ); } public override bool CanFly() { return true; } // Error! public override int Age() { return 3; } }
我有几个错误(到目前为止):
在我的班级 AnimalBase
中,我收到错误消息'AnimalBase.NumLegs() - 找不到合适的方法来覆盖'。为什么? AnimalBase
实现 IAnimal
那么为什么它不能在界面中看到方法声明呢?
为什么 AnimalBase
中的抽象 Age()
好吗?我在这里猜测我只是声明一个与界面中的声明无关的全新方法,但是我不应该得到某种警告,我有点隐藏界面声明吗? />
第二个错误是完全相同的'Sparrow.CanFly() - 找不到合适的方法来覆盖'。为什么呢? Sparrow
实现 IBird
,为什么它不能看到 IBird.CanFly()
?
任何帮助理解这可能会节省我剩余的头发。
我尝试了什么:
我已经阅读并阅读了,我发现了很多可行的例子,但没有解释为什么我是试图做的显然是错误的。
I have a couple of errors (so far):
In my class AnimalBase
, I get an error saying 'AnimalBase.NumLegs() - No suitable method found to override'. Why? AnimalBase
implements IAnimal
so why can't it see the method declaration in the interface?
And why is the abstract Age()
in AnimalBase
okay? I am guessing here that I am simply declaring a brand new method that has nothing to do with the declaration in the interface, but shouldn't I get some kind of warning that I am kind-of 'hiding' the interface declaration?
The second error is exactly the same 'Sparrow.CanFly() - No suitable method found to override'. Again why? Sparrow
implements IBird
, so why can't it see IBird.CanFly()
?
Any help understanding this might save my remaining hair.
What I have tried:
I have read and read, and I have found lots of examples that do work but no explanation of why what I am trying to do is apparently wrong.
好吧,你的抽象类声明了一个覆盖
成员NumLegs - 它没有基类实现来覆盖。你需要的是将它声明为virtual
,它允许派生类覆盖它:
Well, your abstract class declares anoverride
member NumLegs - which has no base class implementation to override. What you need is to declare it asvirtual
which allows derived classes to override it:
public virtual int NumLegs() { return 4; }
如果他们这样做,那么将调用新版本,如果他们没有,那么将使用基类版本,并将返回4。
CanFly存在同样的问题:接口方法不能被覆盖,因为它们没有;要有基类实现来覆盖。在这种情况下,只需删除关键字:
If they do, then the "new" version will be called, if they don't, then the base class version will be used, and 4 will be returned.
The same problem exists with CanFly: interface methods cannot be overridden as they don;t have a base class implementation to override. In this case, just remove the keyword:
public bool CanFly() { return true; }
,你的错误就会消失。
and your errors will disappear.
这篇关于努力理解接口和抽象基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!