相关疑难解决方法(0)

Java中的抽象类不需要从其实现接口实现任何方法.为什么?

我们来看看Java中的以下简单代码片段.

interface Sum
{
    abstract public void showSum();
}

interface Mul
{
    abstract public void showMul();
}

abstract class Super implements Sum
{
    protected int x;
    protected int y;

    public Super(int x, int y)
    {
        this.x=x;
        this.y=y;
    }

    //No error, though the method showSum() of the implementing iterface Sum is commented. Why?

    /*public void showSum()
    {
        System.out.println("Sum = "+(x+y));
    }*/
}

final class Calculation extends Super implements Mul
{
    public Calculation(int x, int y)
    {
        super(x,y);
    }

    public void showSum() …
Run Code Online (Sandbox Code Playgroud)

java

9
推荐指数
1
解决办法
1万
查看次数

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

这不编译.我可以添加一个抽象方法"Tick_Implementation"到CanvasTool_BaseDraw,然后实现ICanvasTool.TickCanvasTool_BaseDraw同一个班轮只是调用Tick_Implementation.这是推荐的解决方法吗?

c# oop abstract-class interface

3
推荐指数
1
解决办法
1196
查看次数

标签 统计

abstract-class ×1

c# ×1

interface ×1

java ×1

oop ×1