首页 > 解决方案 > 相当于Basic的Module

问题描述

我的 Android 应用程序包含一些活动使用的一些功能。此时看起来像:

MainActivity extends Activity {
 ...
 public static int someFunction(int arg1) {
  return arg1 * arg1;
 }
}

从其他活动中,它调用MainActivity.someFunction(arg1). 工作正常,但看起来很丑。我认为 Basic 的构造“模块”(所有成员都是静态的,只有静态构造函数可用,默认情况下,成员调用没有模块的名称)更好,因为允许在“视图”和“引擎”上划分应用程序逻辑。据我所知,Java 没有这样的结构:

module FN {
 public int someFunction(int arg1) {
  return arg1 * arg1;
 }
}

如果它存在,请给我一些建议。

标签: javaandroid

解决方案


You could use static imports to get a similar usage as with Basics modules. As specified here: https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;

or en masse:

import static java.lang.Math.*;

Once the static members have been imported, they may be used without qualification:

double r = cos(PI * theta);


推荐阅读