java - 为什么不使用静态方法?
问题描述
所以,我对 Java 还是很陌生,据我所知,这两者是相同的:
public class HelloWorld {
public void test(String test) {
System.out.println(test);
}
public static void main(String args[]) {
HelloWorld helloworld = new HelloWorld();
helloworld.test("Hello world!");
}
}
和
public class HelloWorld {
public static void test(String test) {
System.out.println(test);
}
public static void main(String args[]) {
test("Hello world!");
}
}
这两者是同一件事吗?您为什么会使用其中一个?
解决方案
静态方法有时很难测试。
非静态方法指定对象的行为。静态方法通常用于实用程序函数(例如Collections.sort()
)
推荐阅读
- tensorflow - 层 conv2d_30 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。收到的完整形状:[None, 621, 2]
- ios - “它没有返回 0,并且 errno 设置为 22”是什么意思?
- c# - 有没有比这更好的方法来创建角色选择和生成器?
- java - 未使用该字段的值
- javascript - 选择桌面捕获源时是否有 Chromium 标志来自动选择共享音频?
- xpath - Xpath,在 Google 表格中获取带有文本和图像的表格
- reactjs - 将数组的发布请求发送到directus
- javascript - 在 Chart.js 上使用垂直线更新折线图
- flutter - Flutter - 如何创建自定义形状
- kendo-ui - 如何将提交按钮重命名为 kendoForm