有条件实现接口 [英] conditionally implement an interface

查看:157
本文介绍了有条件实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图做一些动画在我的应用程序,有一个办法做到这一点,而无需使用 ObjectAnimator AnimatorListener 11 API 。然而,他们不是一样光滑和强大的使用这些类只是在做动画。

So I'm trying to do some animations in my app and had a way to do it without using ObjectAnimator and AnimatorListener from API 11. However, they're not as smooth and robust as just doing animations using those classes.

于是我与API级别的检查改变了我的对象的类更改我的code,使 ObjectAnimator AnimatorListener 的东西就不叫除非API等级11+。

So I made the changes to my code in my object's class with a check for the API level, so that ObjectAnimator and AnimatorListener stuff wouldn't be called unless the API level is 11+.

问题是,我的对象是实现即使它的功能不被在code的所有版本中使用 AnimatorListener 。我认为这是通向一个的VerifyError 开始了我的应用程序因为现在它崩溃与API级别10设备和下降。

The problem is that my object is implementing AnimatorListener even though its functionality isn't being used in all versions of the code. I think this is leading to an VerifyError on start up of my app because now it crashes on devices with API level 10 and down.

有没有办法实现有条件基于API级别或不同的方式来实现同样的事情的接口方式?

Is there a way to conditionally implement an interface based on the API level or a different way to achieve the same thing?

谢谢!

推荐答案

其实,这是确定,如果你筑起或更高的SDK的目标,这个问题只有当你的code是真的在设备上运行失踪类从一个更高的SDK。正如其他人的建议,你可以去工厂

actually, it is OK if you build against or with a higher SDK target, the issue is only when your code is really run on a device missing classes from a higher SDK. As others suggested, you can go for a factory

public class Factory {
  public static <T> T getImplementation(){
    if(<SDK_LEVEL_INCOMPATIBLE>){
      return (T)new <package>.OldSchoolAnimator();
    }else{
      return (T)new <package>.SuperAnimator();
    }
  }
}


...
SomeImplementation impl = new Factory().getImplementation();
...

WATCHOUTSomeImplementation必须OldSchoolAnimator和SuperAnimator类的通用超类型或接口!不使用进口的实现类在你的工厂,而使用全限定名。

watchout, "SomeImplementation" has to be a common super-type or interface of OldSchoolAnimator and SuperAnimator classes! Dont use "imports" for implementation classes in your factory, rather use full-qualified classnames.

这篇关于有条件实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆