我有一个class
(下面)但是每当我尝试编译代码时,我都会收到一条错误消息,指出class
不是abstract
。
任何人都可以解释这意味着什么以及我需要采取哪些不同的做法?
package huntermessenger;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class HunterClient implements Runnable {
private static final String SERVER_IP = "127.0.0.1";
private static final int SERVER_PORT = 5000;
Scanner scanner = new Scanner(System.in); //to read text from the console
Socket socket = null;
public void Run() {
try {
socket = new Socket(SERVER_IP, SERVER_PORT);
System.out.println("Connected to server");
}
catch (Exception ex) {
System.out.println("Error connecting to server: " + ex.getMessage());
}
ObjectInputStream in = null;
ObjectOutputStream out = null;
while (true) {
try {
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
//read a string
System.out.println("Enter a message: ");
String str = scanner.next();
//send it to server
out.writeObject(new Message(str));
out.flush();
//get the reply from the server
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
Message message = (Message) in.readObject();
System.out.println("Server said: " + message.getMessage());
}
catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
}
}
答案 0 :(得分:4)
java区分大小写:
public void Run() {
与
不同public void run() {
您需要实施run()
而不是Run()
。
将其更改为
public void run() {
答案 1 :(得分:0)
public void Run() {}
Run()不应大写。 run()是一个抽象方法,并且您没有覆盖它,因为您实现的是Run()。因此,您的类仍然有一个抽象方法,但该类不是抽象的,因此是错误。