如何组织助手功能 [英] How to organize helper functions

查看:101
本文介绍了如何组织助手功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建许多用于转换字符串的帮助程序例程.

I need to create lot of helper routines for converting strings.

类似的东西:

String Function1(String s) {}

我想从任何Activity打电话给他们.

I would like to call them from any Activity.

做到这一点的最佳方法是什么?我是否需要创建一个类?我当时只是想拥有一个包含所有这些功能的单独文件.这是不是一个包裹的候选人?

What is the best way to do this ? Do I need to create a class or not ? I was thinking just to have one separate file with all these functions. Is this a candidate for a package or not ?

推荐答案

使用公共静态方法创建一个类,然后可以使用ClassName.methodName(parameters)在每个位置调用它们:

Create a class with public static methods, then you can call them every where with ClassName.methodName(parameters):

public class Util {
  public static String methodOne(String param) {
      //do something
      return param;
  }

  public static String methodTwo(String param) {
      //do something
      return param;
  }

  //...
}

在其他班级内:

String someString = Util.methodOne("Some String");
// ...

这篇关于如何组织助手功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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