【问题标题】:How can I give Interface a boolean variable?如何给接口一个布尔变量?
【发布时间】:2016-05-14 18:20:40
【问题描述】:

我的界面是这样的

public interface AndroidStuff {

    public void adShower();
    public void shareActivity();
}

我需要给它一个 boolean watched 并且不对其进行初始化,然后在我实现此接口的主要 Android 类中修改该布尔值,以便我可以从我的非 Android 类中检查它是 false 还是 true(I' m 使用 LibGdx)。

这是AndroidApplication 类:

public class AndroidLauncher extends AndroidApplication implements AndroidStuff {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(this), config);
        HeyzapAds.start("1",this);
        IncentivizedAd.fetch();
    }

    @Override
    public void adShower() {
        IncentivizedAd.display(this);
        IncentivizedAd.fetch();
        IncentivizedAd.setOnIncentiveResultListener(new HeyzapAds.OnIncentiveResultListener() {
            @Override
            public void onComplete(String tag) {
            }

            @Override
            public void onIncomplete(String tag) {

            }
        });
    }

    @Override
    public void shareActivity() {

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

如果onComplete 已被调用,我需要检查非 Android 类。

【问题讨论】:

  • 所有接口变量都是public static和final。所以你不能改变它们。你需要的是一个抽象类
  • 但是我不能实现抽象类?
  • 你可以扩展一个抽象类。
  • 是的,你不能实现那个类,你必须扩展那个类。你需要一些“特征”,比如行为抽象。但不幸的是,这在 Java 中不存在。
  • 我已经扩展了 AndroidApplication 类,但我只需要检查是否观看了广告视频,我会更新问题。

标签: java android interface


【解决方案1】:

你不能在 Java 的接口中放置实例变量,但你可以做的是:

public interface AndroidStuff
{
    boolean getWatched();
    // Your other methods...
}

然后在实现该接口的类中:

public class ExampleClass implements AndroidStuff
{
    private boolean watched;
    @Override
    public boolean getWatched()
    {
        return watched; // Or whatever logic you like
    }
    // The rest of the class...
}

如果您想使用界面,您可能会这样做。否则,只需将 AndroidStuff 设为 abstract class

【讨论】:

  • 但还要注意 Java 不允许多重继承,因此如果您将 AndroidStuff 设为 abstract class,您的子类将无法继承任何其他内容。
  • getWatched();成功了,我也做了同样的事情,但我忘记了我必须归还观看
【解决方案2】:

更新后,onComplete()中会更新的简单静态字段呢?

public class AndroidLauncher extends AndroidApplication implements AndroidStuff {

 public static boolean isWatched;

 // on onComplete() set true;
}

【讨论】:

    【解决方案3】:

    所有接口变量都必须是 public static 和 final。所以你不能改变它们。你需要的是abstract class

    编辑: 在你的情况下;您需要从应用程序类中的 onComplete 回调到其他地方。

    有两种可能的解决方案: 1.你可以像这样使用静态标志变量

    public class AndroidLauncher extends AndroidApplication implements AndroidStuff{
      public static boolean isAdShown = false;
    
    ....onComplete(){
      isAdShown  = true; 
      }
    }
    

    然后像这样在其他类中检查它

    if(AndroidLauncher.isAdShown) ....
    
    1. 在您的应用程序类中定义一个接口,为您提供回调:

          public class AndroidLauncher extends AndroidApplication implements AndroidStuff{
        private CallBack callback;
        public void registerCallBack(Callback callback){
              this.callback = callback;
        }
        public interface CallBack{
              void onAdShown();
        }
      
      }
      

    现在;让您的其他类注册回调并为该回调提供实现;

        public OtherClass implements AndroidLauncher.CallBack{
        public OtherClass(){
         instanceOfApplicationClass.registerCallback(this);
        }
    
         @Override
         public void onAdShown(){
           //here you get the callback when ad is shown
         }
        }
    

    【讨论】:

      【解决方案4】:
      public interface AndroidStuff {
      
         public void adShower();
         public void shareActivity(boolean isChecked);
      }
      

      那么当你在活动中实现它时,你会得到这个变量以供使用

       public class AndroidLauncher extends AndroidApplication implements AndroidStuff {
      
      
      
       @Override
       protected void onCreate (Bundle savedInstanceState) {
      
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(this), config);
        HeyzapAds.start("1",this);
        IncentivizedAd.fetch();
        }
      
        @Override
        public void adShower() {
        IncentivizedAd.display(this);
        IncentivizedAd.fetch();
        IncentivizedAd.setOnIncentiveResultListener(new HeyzapAds.OnIncentiveResultListener() {
          @Override
          public void onComplete(String tag) {
          }
      
          @Override
          public void onIncomplete(String tag) {
      
          }
          });
          }
      
         @Override
         public void shareActivity(boolean isChecked) {
         //here youwill get boolean variable for use
         } 
      
      
      
       @Override
       protected void attachBaseContext(Context base) {
       super.attachBaseContext(base);
       MultiDex.install(this);
       }
      
       @Override
       protected void onResume() {
       super.onResume();
       }
       }
      

      【讨论】:

        猜你喜欢
        • 2021-04-07
        • 2017-06-23
        • 1970-01-01
        • 1970-01-01
        • 2022-10-16
        • 2016-07-22
        • 2020-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多