为什么我的方法未定义类型对象? [英] Why is my method undefined for the type object?
本文介绍了为什么我的方法未定义类型对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定为什么Eclipse会给我这个错误:
I'm not sure why Eclipse is giving me this error:
方法
listen()
未定义类型对象
什么是简单的我做错了吗?另外,我的代码是编写 main
方法的正确方法,该方法实例化 EchoServer0
对象并调用其 listen
方法?
What simple mistake am I making? Also, is my code the right way to write a main
method which instantiates an EchoServer0
object and calls its listen
method?
public class EchoServer0 {
public void listen() {
ServerSocket socket = null;
try{
socket = new ServerSocket(2013);
System.out.println("Opened server socket");
socket.setSoTimeout(2000);
socket.accept();
socket.close();
}
catch (SocketTimeoutException ste){
System.out.println("Timed out after " + 2000 + " ms");
}
catch (Exception e){
System.out.println(e.getClass().getName()+" at server: " + e.getMessage());
}
}
public static void main(String[] args) {
Object EchoServer0;
EchoServer0.listen();
}
}
推荐答案
更改您的主要内容:
public static void main(String[] args) {
EchoServer echoServer = new EchoServer();
echoServer.listen();
}
声明对象EchoServer0;
你有一些错误。
- EchoServer0的类型为Object,因此它没有方法listen()
- 你还需要用
new
- 创建一个实例。另一个问题,这只是关于命名约定,你应该打电话给你变量以小写字母开头,echoServer0而不是EchoServer0。大写名称通常用于类名。
- 您不应创建与其类名相同的变量。令人困惑。
- EchoServer0 is of type Object, therefore it doesn't have the method listen()
- You will also need to create an instance of it with
new
- Another problem, this is only regarding naming conventions, you should call your variables starting by lower case letters, echoServer0 instead EchoServer0. Uppercase names are usually for class names.
- You should not create a variable with the same name than its class. It is confusing.
这篇关于为什么我的方法未定义类型对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文