如何重用接口多个抽象方法中的一个或多个?

2016-11-07 22:56 评论 0 条

摘要:

接口的好处,只需要把参数传递过来,具体需要做什么用或如何处理,全权交给实现类,一个接口可以有多个实现类,每个实现类根据自身的需要实现不同的功能,相信每个Android开发的程序员都或多或少自定义并使用过接口。现在我们来讨论,如何常用一个口中的某个方法?首先,明白一个原理,一个接口如果定义了多个接口方法,比如5个接口方法,那么实现类中必须同时重写5个方法,在5个方法中我只想重用其中的一个或多个方法,有没有想过这个问题呢?

重用接口定义的抽象方法

接口只能定义抽象方法,通常省略关键字:abstract,同时不允许声明变量和常量,想要重用接口中的其中一个抽象方法,直接实现是行不通的。最近在学习属性动画的时候看到了一个抽象类:AnimatorListenerAdapter,该类继承Object,实现了AnimatorListener和AnimatorPauseListener两个接口,然后发现在给ValueAnimator添加AnimatorListener的时候,可以直接传递AnimatorListenerAdapter,然后重写上述抽象类其中的一个方法,代码如下:

  1. valueAnimator.addListener(new AnimatorListenerAdapter() {  
  2.                 @Override  
  3.                 public void onAnimationEnd(Animator animation) {  
  4.                     balls.remove(((ObjectAnimator)animation).getTarget());  
  5.   
  6.                 }  
  7. });  

如果添加的监听器是AnimatorListener,需要实现onAnimationStart,onAnimationEnd,onAnimationCancel和onAnimationRepeat四个抽象方法,感觉有的方法不需要使用到,代码如下:

  1. valueAnimator.addListener(new Animator.AnimatorListener() {  
  2.                 @Override  
  3.                 public void onAnimationStart(Animator animation) {  
  4.   
  5.                 }  
  6.   
  7.                 @Override  
  8.                 public void onAnimationEnd(Animator animation) {  
  9.   
  10.                 }  
  11.   
  12.                 @Override  
  13.                 public void onAnimationCancel(Animator animation) {  
  14.   
  15.                 }  
  16.   
  17.                 @Override  
  18.                 public void onAnimationRepeat(Animator animation) {  
  19.   
  20.                 }  
  21. });  

从上面的对比中,发现了使用AnimatorListenerAdapter的好处是:有选择地重写需要使用到的方法,比如上文中的onAnimationEnd方法,对于没有使用到的onAnimationStart,onAnimationCancel和onAnimationRepeat可以不用重写,当然也可以选择重写其中的一个或多个方法,代码就显得干净利索了很多。

为什么定义一个抽象类实现接口,就可以有选择地重写其中一个或多个方法呢?

首先,定义的AnimatorListenerAdapter抽象类也属于接口AnimatorListener和接口AnimatorUpdateListener的实现类,只是实现类再重写的抽象方法里不做任何的逻辑处理,原封不动地重写实现接口中的所有方法,然后查看AnimatorListenerAdapter的源码,你会发现里面不做任何业务处理,仅仅重写了AnimatorListener的四个方法和AnimatorUpdateListener的两个方法,源码如下:

  1. /* 
  2.  * Copyright (C) 2010 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package android.animation;  
  18.   
  19. /** 
  20.  * This adapter class provides empty implementations of the methods from {@link android.animation.Animator.AnimatorListener}. 
  21.  * Any custom listener that cares only about a subset of the methods of this listener can 
  22.  * simply subclass this adapter class instead of implementing the interface directly. 
  23.  */  
  24. public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,  
  25.         Animator.AnimatorPauseListener {  
  26.   
  27.     /** 
  28.      * {@inheritDoc} 
  29.      */  
  30.     @Override  
  31.     public void onAnimationCancel(Animator animation) {  
  32.     }  
  33.   
  34.     /** 
  35.      * {@inheritDoc} 
  36.      */  
  37.     @Override  
  38.     public void onAnimationEnd(Animator animation) {  
  39.     }  
  40.   
  41.     /** 
  42.      * {@inheritDoc} 
  43.      */  
  44.     @Override  
  45.     public void onAnimationRepeat(Animator animation) {  
  46.     }  
  47.   
  48.     /** 
  49.      * {@inheritDoc} 
  50.      */  
  51.     @Override  
  52.     public void onAnimationStart(Animator animation) {  
  53.     }  
  54.   
  55.     /** 
  56.      * {@inheritDoc} 
  57.      */  
  58.     @Override  
  59.     public void onAnimationPause(Animator animation) {  
  60.     }  
  61.   
  62.     /** 
  63.      * {@inheritDoc} 
  64.      */  
  65.     @Override  
  66.     public void onAnimationResume(Animator animation) {  
  67.     }  
  68. }  

理解了抽象类实现接口的好处了之后,我们再来看看Android源码里面,还有哪些类使用这样的一种原理的呢?对的,想到了:SimpleOnGestureListener和OnGestureListener,给一个View添加手势监听器的时候,通常会有选择地重写SimpleOnGestureListener的方法,很少重写OnGestureListener接口的所有方法,在这里SimpleOnGestureListener是一个static声明的类,比如我们只需要重写onDown()方法,代码如下:

  1. GestureDetector.SimpleOnGestureListener listener=new GestureDetector.SimpleOnGestureListener(){  
  2.             @Override  
  3.             public boolean onDown(MotionEvent e) {  
  4.                 return super.onDown(e);  
  5.             }  
  6.         };  

那么回到文章开头提到的问题,如何重用一个接口定义的其中一个方法,那就很简单了,只需要声明一个abstract类或一个static类实现接口方法,然后在使用到的地方传入abstract类或static类对象,最后有选择地重用其中一个或多个方法,是不是对接口的理解又提升了一个台阶?

当前文章价值6.54元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://www.teachcourse.cn/2071.html ,谢谢支持!

资源分享

分类:Android 标签:, ,
Python监听多个异步任务通知并依次处理通知,一次只能处理一个通知 Python监听多个异步任务通知并依
Python框架Flash_Restful安装使用 Python框架Flash_Restful安装
成员变量的隐藏和方法的重写 成员变量的隐藏和方法的重写
Android面试笔记二:果肉教育 Android面试笔记二:果肉教育

发表评论

呲牙 憨笑 坏笑 偷笑 色 微笑 抓狂 睡觉 酷 流汗 鼓掌 大哭 可怜 疑问 晕 惊讶 得意 尴尬 发怒 奋斗 衰 骷髅 啤酒 吃饭 礼物 强 弱 握手 OK NO 勾引 拳头 差劲 爱你

表情