java– 在没有成员变量的情况下创建抽象类的重点是什么?

java– 在没有成员变量的情况下创建抽象类的重点是什么?,第1张

概述这个问题实际上比主题更具体,但我认为这个主题通常涵盖了我想知道的内容.我正在审查一个看起来像我想要使用的项目的一些代码,我看到了一些对我感兴趣的东西,因为我不确定为什么要这样做.具体来说,在ChrisBanes的ActionBar-PullToRefresh中,我在.java中看到了这个:packageuk.co

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

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

package uk.co.senab.actionbarpulltorefresh.library;@R_301_5565@ androID.app.Activity;@R_301_5565@ androID.content.res.Configuration;@R_301_5565@ 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 – 在没有成员变量的情况下创建抽象类的重点是什么?全部内容,希望文章能够帮你解决java – 在没有成员变量的情况下创建抽象类的重点是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1110402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存