public static void main(String[] args) { MobileSystem system = new MobileSystem(); system.run(); }是什么意思
时间: 2024-05-17 09:17:49 浏览: 9
这是一个 Java 语言的程序入口方法,其中定义了一个 MobileSystem 类型的对象 system,并调用它的 run() 方法来启动移动设备系统。在 Java 语言中,所有的程序都必须从 main() 方法开始执行。当程序运行时,Java 虚拟机会自动调用 main() 方法,并将运行时的参数传递给它,这些参数保存在 args 数组中。
相关问题
import java.util.Scanner; public class Main { public static void main(String[] args) { Thread t = new Thread(new RunHandler()); t.run(); } } class RunHandler { public void run() { Scanner in = new Scanner(System.in); int x = in.nextInt(); System.out.println("run"); } }
这段代码有一个小错误,应该使用 `t.start()` 方法启动线程,而不是 `t.run()` 方法。`t.run()` 方法只是在当前线程中直接调用了 `run()` 方法,不会创建新的线程,因此程序不会输出 "run"。应该将 `main` 方法中的 `t.run()` 改为 `t.start()`,代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler());
t.start(); // 启动新线程
}
}
class RunHandler implements Runnable {
public void run() {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
System.out.println("run");
}
}
```
这样修改后,程序将会启动一个新线程,并且能够输出 "run"。
public class Test extend Thread(){ String str[]=new String[10]; static { for (int i = 0; i < str.length; i++) { str[i]=i+"-"; } } public void start() { try { for (int i = 0; i < str.length; i++) { sleep(1000); System.out.print(str[i]); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String args[]) { Test t = new Test(); t.run(); } }
这段代码有几个问题:
1. 类名应该是 `Test extends Thread` 而不是 `Test extend Thread()`
2. `start()` 方法应该是重写 `Thread` 类的 `start()` 方法,而不是定义一个新的 `start()` 方法
3. 在 `main` 方法中应该调用 `t.start()` 来启动线程,而不是 `t.run()`,因为后者只是普通的方法调用,不会创建新的线程,而是在当前线程中执行线程代码。
修改后的代码如下:
```
public class Test extends Thread {
String str[]=new String[10];
static {
for (int i = 0; i < str.length; i++) {
str[i]=i+"-";
}
}
@Override
public void run() {
try {
for (int i = 0; i < str.length; i++) {
sleep(1000);
System.out.print(str[i]);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Test t = new Test();
t.start();
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)