【问题标题】:What is the point of creating an Abstract Class over implementation with no member variables?在没有成员变量的实现上创建抽象类有什么意义?
【发布时间】:2013-12-02 06:55:38
【问题描述】:

这个问题实际上比主题更具体,但我认为该主题通常涵盖了我想知道的内容。

我正在审查一个项目中的一些代码,这些代码看起来像是我想要使用的东西,我看到了一些让我感兴趣的东西,因为我不确定为什么要这样做。具体来说,在 Chris Banes 的 ActionBar-PullToRefresh 中,我在 .java 中看到了这一点:

package uk.co.senab.actionbarpulltorefresh.library;

import android.app.Activity;
import android.content.res.Configuration;
import android.view.View;

/**
* HeaderTransformers are what controls and update the Header View to reflect the current state
* of the pull-to-refresh interaction. They are responsible for showing and hiding the header
* view, as well as update the state.
*/
public abstract class HeaderTransformer {

   /**
  * Called whether the header view has been inflated from the resources
 * defined in {@link Options#headerLayout}.
 *
 * @param activity The {@link android.app.Activity} that the header view is attached to.
 * @param headerView The inflated header view.
 */
public void onViewCreated(Activity activity, View headerView) {}

/**
 * Called when the header should be reset. You should update any child
 * views to reflect this.
 * <p/>
 * You should <strong>not</strong> change the visibility of the header
 * view.
 */
public void onReset() {}

/**
 * Called the user has pulled on the scrollable view.
 *
 * @param percentagePulled value between 0.0f and 1.0f depending on how far the
 *                         user has pulled.
 */
public void onPulled(float percentagePulled) {}

/**
 * Called when a refresh has begun. Theoretically this call is similar
 * to that provided from {@link uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener} but is more suitable
 * for header view updates.
 */
public void onRefreshStarted() {}

/**
 * Called when a refresh can be initiated when the user ends the touch
 * event. This is only called when {@link Options#refreshOnUp} is set to
 * true.
 */
public void onReleaseToRefresh() {}

/**
 * Called when the current refresh has taken longer than the time
 * specified in {@link Options#refreshMinimizeDelay}.
 */
public void onRefreshMinimized() {}

/**
 * Called when the Header View should be made visible, usually with an animation.
 *
 * @return true if the visibility has changed.
 */
public abstract boolean showHeaderView();

/**
 * Called when the Header View should be made invisible, usually with an animation.
 *
 * @return true if the visibility has changed.
 */
public abstract boolean hideHeaderView();

/**
 * Called when the Activity's configuration has changed.
 *
 * @param activity The {@link android.app.Activity} that the header view is attached to.
 * @param newConfig New configuration.
 *
 * @see android.app.Activity#onConfigurationChanged(android.content.res.Configuration)
 */
public void onConfigurationChanged(Activity activity, Configuration newConfig) {}
}

我对这个文件的问题是为什么在这里做一个抽象类而不是一个接口,或者做一个抽象类的目的是什么?我看到该类有两个抽象方法。我的理解是抽象方法必须在子类中定义,否则子类也是抽象类对吗?那么这是作为一个抽象类完成的,以便仅强制执行这两个抽象方法吗?这是在这里做一个抽象类而不是接口的唯一原因吗?

【问题讨论】:

    标签: java android interface abstract-class


    【解决方案1】:

    使它成为一个抽象类可能有很多原因,其中一些你自己指出了。

    在这种情况下,我的猜测是因为它是一个库。 假设您有一个巨大的应用程序,并使用这个类来定义许多其他类。

    如果通过添加新方法来更新类,那么如果它是接口,您的代码将无法编译,因为您的代码违反了接口的“契约”(它没有实现所有方法)。但在这种情况下,它是一个抽象类,可以定义默认行为:在这种情况下,什么也不做。这样您的应用程序就不会中断,并且可以毫无问题地编译。

    【讨论】:

      【解决方案2】:

      使用abstractinterfaces 是)的原因是能够定义abstract 方法。不过,这不是interface 的原因是能够定义可选 方法。

      下面的构造,例如

      public void onReset() {}
      

      定义(概念上)一个几乎-abstract 方法,它有一个默认的 no-op 实现。

      【讨论】:

        【解决方案3】:

        主要优点可能是您只需要实现子类所需的方法,而接口则需要您实现每个方法,即使您的实现是空的。

        【讨论】:

          猜你喜欢
          • 2014-10-30
          • 2021-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-06
          • 1970-01-01
          相关资源
          最近更新 更多