简体   繁体   English

我的抽象类实现了一个接口,但没有实现它的一些方法。 我如何编译?

[英]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.TickCanvasTool_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.

相关问题 如何列出实现抽象类和接口(C#)的东西? - How to make a list of something that implements abstract class and interface (C#)? 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T 如何从实现这两个接口的类访问在两个接口之间定义的参数和方法? - How do I access parameters and methods defined across two interface from a class that implements those two interfaces? 接口迫使抽象类实现其功能 - Interface is forceing abstract class to implement its functions 我如何解决错误“class_name doesn't implement interface_name”? - How do i solve the error "class_name doesn't implement interface_name"? 如何检查类是否在NRefactory中实现接口方法 - How to check if a class implements interface methods in NRefactory 公共抽象类中实现内部接口的抽象方法不能编译? - Abstract method in public abstract class implementing an internal interface doesn't compile? 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 如何在我的实现IEnumerable的Dictionary包装器类中实现IEnumerable <Foo> ? - How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable<Foo>? 如何将扩展助手方法实现为具有可变参数的抽象类? - How do I implement extension helper methods as an abstract class with variable parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM