zoukankan      html  css  js  c++  java
  • 第五章(6)Libgdx应用框架之接口

    Android游戏开发群:290051794
    Libgdx游戏开发框架交流群:261954621 

     

    有时访问特定平台的API很有必要举例(http://swarmconnect.com/)。

    下面这些例子纯属虚构,假设我们想使用一个只在Android上有的简单的leaderboard API,对其他平台只是简单日志或者模拟返回值。

    Android API看起来像这样:

     

     

     

     

     

    /** Let's assume this is the API provided by Swarm **/
    
    public class LeaderboardServiceApi {
    
       public void submitScore(String user, int score) { ... }
    
    }
    


     

     

     

     

     

    第一步是创建一个抽象的API接口。

    将接口放到Main项目中:

     

     

     

     

     

    public interface Leaderboard {
    
       public void submitScore(String user, int score);
    
    }
    


     

     

     

     

     

    接下来为Android实现这些项目:

     

     

     

     

     

    /** Android implementation, can access LeaderboardServiceApi directly **/
    
    public class AndroidLeaderboard implements Leaderboard {
    
       private final LeaderboardServiceApi service;
    
     
    
       public AndroidLeaderboard() {
    
          // Assuming we can instantiate it like this
    
          service = new LeaderboardServiceApi();
    
       }
    
     
    
       public void submitScore(String user, int score) {
    
          service.submitScore(user, score);
    
       }
    
    }
    


     

     

     

     

     

    接下来,ApplicationListener获取一个构造函数实现leaderboard

     

     

     

     

     

    public class MyGame implements ApplicationListener {
    
       private final Leaderboard leaderboard;
    
     
    
       public MyGame(Leaderboard leaderboard) {
    
          this.leaderboard = leaderboard;
    
       }
    
     
    
       // rest omitted for clarity
    
    }
    


     

     

     

     

     

    在启动类里创建MyGame示例,通过相应的leaderboard作为参数。

     

     作者:宋志辉 
    出处:http://blog.csdn.net/song19891121
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 
    支持: 新浪微博 腾讯微博

     

     

     

     

  • 相关阅读:
    扩展中国剩余定理
    bzoj 3816&&uoj #41. [清华集训2014]矩阵变换
    THUSC2017
    bzoj 4521: [Cqoi2016]手机号码
    bzoj 4871: [Shoi2017]摧毁“树状图”
    bzoj 2300 : [HAOI2011]防线修建
    bzoj 3853 : GCD Array
    HEOI 2017 游记
    bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机模板
    bzoj 4310 跳蚤
  • 原文地址:https://www.cnblogs.com/hainange/p/6153561.html
Copyright © 2011-2022 走看看