Java 为什么我的方法没有为类型对象定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21246489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:38:10  来源:igfitidea点击:

Why is my method undefined for the type object?

javasockets

提问by user3205160

I'm not sure why Eclipse is giving me this error:

我不知道为什么 Eclipse 给我这个错误:

The method listen()is undefined for the type Object

listen()类型的方法未定义Object

What simple mistake am I making? Also, is my code the right way to write a mainmethod which instantiates an EchoServer0object and calls its listenmethod?

我犯了什么简单的错误?另外,我的代码是否是编写main实例化EchoServer0对象并调用其listen方法的方法的正确方法?

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();
    } 
}

回答by poitroae

It should be like that

应该是这样

public static void main(String[] args) {
        EchoServer0 e = new EchoServer0();
        // TODO Auto-generated method stub
        e.listen();
}

Your variable of type Objecttruly doesn't have such a method, but the type EchoServer0you define above certainly has.

你的类型变量Object确实没有这样的方法,但是EchoServer0你上面定义的类型肯定有。

回答by Dawood ibn Kareem

Try this.

尝试这个。

public static void main(String[] args) {
    EchoServer0 myServer;
    myServer = new EchoServer0();
    myServer.listen();
}

What you were trying to do was declaring a variable of type Object, not creating anything for that variable to reference, then trying to call a method that didn't exist (in the class Object) on an object that hadn't been created. It was never going to work.

您试图做的是声明一个 type 变量Object,而不是为该变量创建任何要引用的内容,然后尝试在Object尚未创建的对象上调用不存在(在类中)的方法。它永远不会奏效。

回答by Raul Guiu

Change your main to:

将您的主要更改为:

public static void main(String[] args) {
    EchoServer echoServer = new EchoServer();
    echoServer.listen();
}

When you declare Object EchoServer0;you have a few mistakes.

当你声明时,Object EchoServer0;你有一些错误。

  1. EchoServer0 is of type Object, therefore it doesn't have the method listen().
  2. You will also need to create an instance of it with new.
  3. Another problem, this is only regarding naming conventions, you should call your variables starting by lower case letters, echoServer0 instead of EchoServer0. Uppercase names are usually for class names.
  4. You should not create a variable with the same name as its class. It is confusing.
  1. EchoServer0 是 Object 类型,因此它没有方法 listen()。
  2. 您还需要使用new.
  3. 另一个问题,这仅与命名约定有关,您应该以小写字母开头的变量调用 echoServer0 而不是 EchoServer0。大写名称通常用于类名。
  4. 您不应创建与其类同名的变量。这令人困惑。

回答by Eric Jablow

The line

线

Object EchoServer0;

says that you are allocating an Objectnamed EchoServer0. This has nothing to do with the class EchoServer0. Furthermore, the object is not initialized, so EchoServer0is null. Classes and identifiers have separate namespaces. This will actually compile:

说你正在分配一个Object命名的EchoServer0. 这与班级无关EchoServer0。此外,对象未初始化,EchoServer0也是null。类和标识符具有单独的命名空间。这将实际编译:

String String = "abc";  // My use of String String was deliberate.

Please keep to the Java naming standards: classes begin with a capital letter, identifiers begin with a small letter, constants and enums are all-capitals.

请遵守 Java 命名标准:类以大写字母开头,标识符以小写字母开头,常量和enums 都是大写字母。

public final String ME = "Eric Jablow";
public final double GAMMA = 0.5772;
public enum Color { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET}
public COLOR background = Color.RED;