摘要:
接口的好处,只需要把参数传递过来,具体需要做什么用或如何处理,全权交给实现类,一个接口可以有多个实现类,每个实现类根据自身的需要实现不同的功能,相信每个Android开发的程序员都或多或少自定义并使用过接口。现在我们来讨论,如何常用一个口中的某个方法?首先,明白一个原理,一个接口如果定义了多个接口方法,比如5个接口方法,那么实现类中必须同时重写5个方法,在5个方法中我只想重用其中的一个或多个方法,有没有想过这个问题呢?
重用接口定义的抽象方法
接口只能定义抽象方法,通常省略关键字:abstract,同时不允许声明变量和常量,想要重用接口中的其中一个抽象方法,直接实现是行不通的。最近在学习属性动画的时候看到了一个抽象类:AnimatorListenerAdapter,该类继承Object,实现了AnimatorListener和AnimatorPauseListener两个接口,然后发现在给ValueAnimator添加AnimatorListener的时候,可以直接传递AnimatorListenerAdapter,然后重写上述抽象类其中的一个方法,代码如下:
- valueAnimator.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- balls.remove(((ObjectAnimator)animation).getTarget());
- }
- });
如果添加的监听器是AnimatorListener,需要实现onAnimationStart,onAnimationEnd,onAnimationCancel和onAnimationRepeat四个抽象方法,感觉有的方法不需要使用到,代码如下:
- valueAnimator.addListener(new Animator.AnimatorListener() {
- @Override
- public void onAnimationStart(Animator animation) {
- }
- @Override
- public void onAnimationEnd(Animator animation) {
- }
- @Override
- public void onAnimationCancel(Animator animation) {
- }
- @Override
- public void onAnimationRepeat(Animator animation) {
- }
- });
从上面的对比中,发现了使用AnimatorListenerAdapter的好处是:有选择地重写需要使用到的方法,比如上文中的onAnimationEnd方法,对于没有使用到的onAnimationStart,onAnimationCancel和onAnimationRepeat可以不用重写,当然也可以选择重写其中的一个或多个方法,代码就显得干净利索了很多。
为什么定义一个抽象类实现接口,就可以有选择地重写其中一个或多个方法呢?
首先,定义的AnimatorListenerAdapter抽象类也属于接口AnimatorListener和接口AnimatorUpdateListener的实现类,只是实现类再重写的抽象方法里不做任何的逻辑处理,原封不动地重写实现接口中的所有方法,然后查看AnimatorListenerAdapter的源码,你会发现里面不做任何业务处理,仅仅重写了AnimatorListener的四个方法和AnimatorUpdateListener的两个方法,源码如下:
- /*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package android.animation;
- /**
- * This adapter class provides empty implementations of the methods from {@link android.animation.Animator.AnimatorListener}.
- * Any custom listener that cares only about a subset of the methods of this listener can
- * simply subclass this adapter class instead of implementing the interface directly.
- */
- public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,
- Animator.AnimatorPauseListener {
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAnimationCancel(Animator animation) {
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAnimationEnd(Animator animation) {
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAnimationRepeat(Animator animation) {
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAnimationStart(Animator animation) {
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAnimationPause(Animator animation) {
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAnimationResume(Animator animation) {
- }
- }
理解了抽象类实现接口的好处了之后,我们再来看看Android源码里面,还有哪些类使用这样的一种原理的呢?对的,想到了:SimpleOnGestureListener和OnGestureListener,给一个View添加手势监听器的时候,通常会有选择地重写SimpleOnGestureListener的方法,很少重写OnGestureListener接口的所有方法,在这里SimpleOnGestureListener是一个static声明的类,比如我们只需要重写onDown()方法,代码如下:
- GestureDetector.SimpleOnGestureListener listener=new GestureDetector.SimpleOnGestureListener(){
- @Override
- public boolean onDown(MotionEvent e) {
- return super.onDown(e);
- }
- };
那么回到文章开头提到的问题,如何重用一个接口定义的其中一个方法,那就很简单了,只需要声明一个abstract类或一个static类实现接口方法,然后在使用到的地方传入abstract类或static类对象,最后有选择地重用其中一个或多个方法,是不是对接口的理解又提升了一个台阶?
你可能感兴趣的文章
转载请注明出处: https://www.teachcourse.cn/2071.html ,谢谢支持!