[英]My abstract class implements an interface but doesn't implement some of its methods. How do I make it compile?
interface ICanvasTool
{
void Motion(Point newLocation);
void Tick();
}
abstract class CanvasTool_BaseDraw : ICanvasTool
{
protected abstract void PaintAt(Point location);
public override void Motion(Point newLocation)
{
// implementation
}
}
class CanvasTool_Spray : CanvasTool_BaseDraw
{
protected abstract void PaintAt(Point location)
{
// implementation
}
public override void Tick()
{
// implementation
}
}
This doesn't compile. 这不编译。 I could add an abstract method "Tick_Implementation" to CanvasTool_BaseDraw, then implement
ICanvasTool.Tick
in CanvasTool_BaseDraw
with a one-liner that just calls Tick_Implementation. 我可以添加一个抽象方法“Tick_Implementation”到CanvasTool_BaseDraw,然后实现
ICanvasTool.Tick
在CanvasTool_BaseDraw
有一个班轮只是调用Tick_Implementation。 Is this the recommended workaround? 这是推荐的解决方法吗?
The way to do this is to add an abstract void Tick()
method to CanvasTool_BaseDraw
and override it in CanvasTool_Spray
. 这样做的方法是向
CanvasTool_BaseDraw
添加一个抽象的void Tick()
方法,并在CanvasTool_Spray
覆盖它。
Not every programming language does it this way. 并非每种编程语言都是这样做的。 In Java you do not have to add an abstract method for every method in the interface(s) you implement.
在Java中,您不必为您实现的接口中的每个方法添加抽象方法。 In that case your code would compile.
在这种情况下,您的代码将编译。
You have a few things mixed up.. 你有一些东西混在一起..
Motion should be virtual in your base class so that it may be overridden in child classes. Motion应该在您的基类中是虚拟的,以便可以在子类中重写它。 Your child class needs to make PaintAt override instead of abstract.
您的子类需要使用PaintAt覆盖而不是抽象。 The base class needs to implement Tick as an abstract method.
基类需要将Tick作为抽象方法实现。
interface ICanvasTool
{
void Motion(Point newLocation);
void Tick();
}
abstract class CanvasTool_BaseDraw : ICanvasTool
{
protected abstract void PaintAt(Point location);
public virtual void Motion(Point newLocation)
{
// implementation
}
public abstract void Tick();
}
class CanvasTool_Spray : CanvasTool_BaseDraw
{
protected override void PaintAt(Point location)
{
// implementation
}
public override void Tick()
{
// implementation
}
}
An alternative is don't list the interface in the base classes declaration. 另一种方法是不在基类声明中列出接口。 Each derived class must list ICanvasTool in its declaration if it wants to be implementing the interface and then it is solely responsible for implementing the rest of the interface.
每个派生类必须在其声明中列出ICanvasTool,如果它想要实现接口,那么它只负责实现接口的其余部分。 One drawback I can see is you can't explicitly implement the interface methods in the base class (ie no ICanvasTool:Motion), but otherwise this is a fairly low overhead version.
我可以看到的一个缺点是你不能在基类中显式实现接口方法(即没有ICanvasTool:Motion),但是否则这是一个相当低的开销版本。
public interface ICanvasTool
{
void Motion(Point newLocation);
void Tick();
}
public abstract class CanvasTool_BaseDraw
{
public void Motion(Point newLocation)
{
//some implementation
}
}
public class CanvasTool_Spray : CanvasTool_BaseDraw, ICanvasTool
{
public void Tick()
{
//some implementation
}
}
Note: I left out PaintAt because it wasn't necessary for the example. 注意:我遗漏了PaintAt,因为这个例子没有必要。
As was stated before, an interface is a contract and therefore all of it needs to be implemented. 如前所述,接口是合同,因此所有接口都需要实现。 If aa consumer tried to call something that is defined in the interface but not implemented in the concrete class the application would crash.
如果消费者试图调用界面中定义但未在具体类中实现的内容,则应用程序将崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.