类继承,强制新类实现某些功能
好吧,我正在搞一些事情,特别是接口。
假设我有一个类“Cat”,其基础为“Animal”,动物中有一个方法,如下所示,
public virtual void Walk()
{
// Do walking stuff
}
那么 Cat 会用以下内容覆盖它:
public override void Walk()
{
// Do cat specific walking stuff
}
简单吧?
不过,这是我的问题,有没有办法强制 cat 覆盖基本 Walk() 方法?因此,如果另一个开发人员添加了 Dog 类,他们将被迫实现自己的 Walk 方法(即使它只是 base.Walk())?
所以接口可以解决这个问题,这就是我尝试过的
Cat : Animal : Interface Animal 必须实现 Walk 方法,但 Cat 不需要
Cat : Animal, Interface Cat 必须实现 Walk 方法,但如果开发人员没有添加或忘记了“,Interface”,那么它就会“破坏”它。
有人可以给我一些指导来解决这个问题吗?
谢谢。
编辑1
这就是我的目标,我希望它能让它更清楚。
public class Animal
{
public Animal()
{
Console.WriteLine("Animal");
}
public virtual void Walk()
{
}
}
public class Cat : Animal
{
public Cat() : base()
{
Console.WriteLine("Cat");
}
public override void Walk()
{
}
}
class Dog : Animal
{
public Dog()
{
}
public override void Walk()
{
// Dog implementation
// and / or calls base method
base.Walk();
}
}
这会产生一个错误
class Dog : Animal
{
public Dog()
{
}
}
Ok so i'm messing around with a few things, specifically interfaces.
Say I have a class 'Cat' with its base as 'Animal' Animal has a method in it like so
public virtual void Walk()
{
// Do walking stuff
}
So Cat would override it with:
public override void Walk()
{
// Do cat specific walking stuff
}
Simple right?
Here's my question though, is there a way to force cat to override the base Walk() method? So if another developer added a Dog class they would be forced to implement their own Walk method (even if it was just base.Walk()) ?
So interfaces kind of solves this, this is what i've tried
Cat : Animal : Interface
Animal has to implement the Walk method, but Cat doesn't
Cat : Animal, Interface
Cat has to implement the Walk method, but if the developer doesn't add or forgets the ',Interface' then it will 'break' it.
can someone give me some pointer as to go about this ?
Thanks.
Edit 1
Here's what I am aiming for, i hope it makes it clearer.
public class Animal
{
public Animal()
{
Console.WriteLine("Animal");
}
public virtual void Walk()
{
}
}
public class Cat : Animal
{
public Cat() : base()
{
Console.WriteLine("Cat");
}
public override void Walk()
{
}
}
class Dog : Animal
{
public Dog()
{
}
public override void Walk()
{
// Dog implementation
// and / or calls base method
base.Walk();
}
}
This would create an error
class Dog : Animal
{
public Dog()
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是当您想要将基类和基方法标记为抽象时。
派生类必须实现
Walk
才能实例化。注意:此更改使得
Animal
无法自行实例化,对它的所有引用都将通过更多派生类进行。这就是你们之间存在脱节或相反目标的地方。为了强迫孩子们实现一个方法,你可以将它标记为抽象的。为了允许子进程选择使用基本实现,它需要是虚拟的,这样就可以选择重写它。即使您基本上使用模板方法模式并使算法的部分抽象以将实现推入较低的类,问题仍然是一样的:您不能在保留默认实现的同时强制覆盖.*
您需要确定您是否想要部分或全部基本实现。
*理论上你应该有
abstract void Walk(); protected void WalkImpl() { }
在基础中,这将允许子级在不想提供自己的实现时选择调用WalkImpl
。不过,我不确定我对此有何感受。通过强制覆盖,同时仍然允许派生类的作者使用默认行为,基本上使派生类的作者的生活变得更加困难。 如果可以使用默认行为,只需使用
虚拟
,并信任派生类的作者在他们认为合适时进行重写。This is when you want to mark the base class and the base method as
abstract
.Derived classes will have to implement
Walk
in order to be instantiated.Note: this change makes
Animal
not instantiable on its own, all references to it would be via more derived classes.This is where you have a disconnect, or opposing goals. To force children to implement a method, you mark it abstract. To allow the child to elect to use the base implementation, it would need to be virtual, which would then make it optional to override. Even if you were to basically use a template method pattern and make parts of the algorithm abstract to push the implementation into lower classes, the problem remains the same: you cannot force an override while also leaving a default implementation.*
You need to determine if you want to have a base implementation, in part or in whole.
*You chould theoretically have
abstract void Walk(); protected void WalkImpl() { }
in the base, which would allow the children to choose to invokeWalkImpl
if they didn't want to provide their own implementation.I'm not sure how I feel about this, however. You're basically making the derived classes' authors' lives more difficult by forcing an override while still allowing them to use a default behavior. If the default behavior can be used, simply go with
virtual
and trust authors of derived classes to override when they feel it is appropriate.