我已经用 Java 编程有一段时间了,但它始终只是 Android 应用程序,它们不以静态 main 方法开始。我想知道“标准”Java 程序的约定,因为大多数时候,我正在调用非静态方法,而这些方法显然不能直接通过 main() 方法来完成。
这是我编写的示例程序(仅打印斐波那契数)。这是一个可以接受的解决方案吗?
public class MainClass {
public static void main(String[] args) {
new MainClass().mainProgram();
}
public void mainProgram() {
System.out.println(fibonacci(10).toString());
// go on with the program
}
// suppose this method needed to be non-static
public Integer fibonacci(int count) {
if (count <= 0) {
return null;
}
if (count == 1 || count == 2) {
return 1;
}
// noinspection
// (suppresses logically impossible null-pointer exception warnings)
return fibonacci(count …Run Code Online (Sandbox Code Playgroud)