我正在尝试用两种语言(Java 和 Scala)编写一个简单的客户端/服务器聊天应用程序。 Java 版本正在运行,唯一的问题是翻译它。 在 Java 中我有这样的代码:
import java.net.*;
import java.io.*;
public class FileServer666 extends Thread{
static Socket clientSocket = null;
static ServerSocket serverSocket= null;
static clientThread t[] = new clientThread[10];
public static void main(String args[]) throws IOException
{
int port_number =1406;
try
{
serverSocket = new ServerSocket(port_number);
}catch(IOException e){System.out.println(e);}
System.out.println("Listening" +port_number);
while(true)
{
try
{
clientSocket=serverSocket.accept();
System.out.println("Akceptuje połaczenie od: "+clientSocket.getInetAddress());
for(int i=0; i<=9; i++)
{
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}catch(IOException e){System.out.println(e);}
}
}
}
这里我有一个问题。如何将这一行翻译成 Scala:
(t[i] = new clientThread(clientSocket,t)).start();
你有什么建议吗?
最佳答案
如果您坚持直接翻译,而您的问题只是在 scala 中分配不返回值,那么就这样做
t(i) = new ClientThread(clientSocket, t)
t(i).start
关于java - 斯卡拉 : how to start an actor in array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229244/