使用抽象方法有什么意义? [英] What is the point of using abstract methods?

查看:33
本文介绍了使用抽象方法有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用抽象方法"有什么意义?抽象类不能被实例化,但是抽象方法呢?他们只是在这里说你必须实现我",如果我们忘记了他们,编译器会抛出错误吗?

What's the point of using "abstract methods"? An abstract class cannot be instantiated, but what about the abstract methods? Are they just here to say "you have to implement me", and if we forget them, the compiler throws an error?

还有别的意思吗?我也读过一些关于我们不必重写相同的代码"的内容,但是在抽象类中,我们只声明"了抽象方法,因此我们将不得不重写子类中的代码.

Does it mean something else? I also read something about "we don't have to rewrite the same code", but in the abstract class, we only "declare" the abstract method, so we will have to rewrite the code in the child class.

你能帮我理解一下吗?我查看了有关抽象类/方法"的其他主题,但没有找到答案.

Can you help me understand it a bit more? I checked the other topics about "abstract class/methods" but I didn't find an answer.

推荐答案

除了提醒你必须实现它之外,最大的好处是任何通过抽象类类型(包括this 在抽象类本身中)可以使用该方法.

Besides the reminder that you have to implement it, the big advantage is that anyone who references the object by its abstract class type (including this in the abstract class itself) can use the method.

例如,假设我们有一个类负责获取状态并以某种方式操纵它.抽象类将负责获取输入,将其转换为 long(例如)并以某种方式将该值与前一个值组合——某种方式"是抽象方法.抽象类可能类似于:

For instance, let's say we have a class responsible for taking state and manipulating it in some way. The abstract class is going to be responsible for getting the input, converting it to a long (for instance) and combining that value with the previous value in some way -- that "some way" is the abstract method. The abstract class may look something like:

public abstract class StateAccumulator {
    protected abstract long accumulate(long oldState, long newState);

    public handleInput(SomeInputObject input) {
        long inputLong = input.getLong();
        state = accumulate(state, inputLong);
    }

    private long state = SOME_INITIAL_STATE;
}

现在您可以定义一个加法累加器:

Now you can define an addition accumulator:

public class AdditionAccumulator extends StateAccumulator {
    @Override
    protected long accumulate(long oldState, long newState) {
        return oldState + newState;
    }
}

如果没有那个抽象方法,基类将无法说以某种方式处理这个状态".不过,我们不想在基类中提供默认实现,因为它没有多大意义——您如何为其他人将实现这个"定义默认实现?

Without that abstract method, the base class would have no way to say "handle this state somehow." We don't want to provide a default implementation in the base class, though, because it wouldn't mean much -- how do you define a default implementation for "someone else will implement this"?

请注意,给猫剥皮的方法不止一种.策略模式 将涉及声明一个声明 accumulate 模式的接口,并将该接口的实例传递给不再抽象的基类.用行话来说,这就是使用组合而不是继承(您已经用两个对象(一个聚合器和一个加法器)组成了一个加法聚合器).

Note that there's more than one way to skin a cat. The strategy pattern would involve declaring an interface that declares the accumulate pattern, and passing an instance of that interface to the no-longer-abstract base class. In lingo terms, that's using composition instead of inheritance (you've composed an addition aggregator out of two objects, an aggregator and an adder).

这篇关于使用抽象方法有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆