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

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

问题描述

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

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"?

请注意,皮肤猫的方法不止一种。 策略模式将涉及声明一个声明累积 pattern,并将该接口的实例传递给不再抽象的基类。用术语来说,那就是使用组合而不是继承(你已经用两个对象,一个聚合器和一个加法器组成了一个加法聚合器)。

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天全站免登陆