gpt4 book ai didi

java - 这个类的作用是什么?

转载 作者:行者123 更新时间:2023-12-01 19:41:03 26 4
gpt4 key购买 nike

“实现 Runnable”是什么意思?我正在将数据从 arduino 发送到这个 java 文件

public static class SerialReader implements Runnable {
InputStream in;
public SerialReader( InputStream in ) {
this.in = in;
}

最佳答案

implements是java中用于实现接口(interface)的关键字。

根据甲骨文:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

Source

关于java - 这个类的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com