java - 相当于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;
}
}
如果它存在,请给我一些建议。
解决方案
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);
推荐阅读
- sql - 使用案例的多重计算
- android - Unity 屏幕有时会在 android 中冻结,有时可以完美运行,而在编辑器中一切正常
- python - 从 -x 到 +x 的solve_ivp
- postgresql - 如何查找当前时区 EST 或 EDT
- css - 为什么将 ::content 更改为 ::slotted(..) 不起作用?
- c++ - std::map::size_type 用于其 value_type 是它自己的 size_type 的 std::map
- python - 神圣 - 将所有参数作为一个传递
- azure - 在 Azure 虚拟机之间共享具有用户权限的驱动器
- python-3.x - 更新本地包内的数据集
- java - 当我期望它时,CORS 的 Spring 实现不会拒绝消息