简体   繁体   English

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

[英]Why is my method undefined for the type object?

I'm not sure why Eclipse is giving me this error:我不知道为什么 Eclipse 给我这个错误:

The method listen() is undefined for the type Object未定义Object类型的方法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?另外,我的代码是否是编写实例化EchoServer0对象并调用其listen方法的main方法的正确方法?

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

Change your main to:将您的主要更改为:

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

When you declare Object EchoServer0;当您声明Object EchoServer0; you have a few mistakes.你有几个错误。

  1. EchoServer0 is of type Object, therefore it doesn't have the method listen(). EchoServer0 是 Object 类型,因此它没有方法 listen()。
  2. You will also need to create an instance of it with new .您还需要使用new创建它的一个实例。
  3. Another problem, this is only regarding naming conventions, you should call your variables starting by lower case letters, echoServer0 instead of EchoServer0.另一个问题,这仅与命名约定有关,您应该以小写字母开头的变量调用 echoServer0 而不是 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.这令人困惑。

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.您试图做的是声明一个Object类型的变量,而不是为该变量创建任何要引用的变量,然后尝试在尚未创建的Object上调用不存在的方法(在Object类中)。 It was never going to work.它永远不会奏效。

The line线

Object EchoServer0;

says that you are allocating an Object named EchoServer0 .说你正在分配一个名为EchoServer0Object This has nothing to do with the class EchoServer0 .这与EchoServer0类无关。 Furthermore, the object is not initialized, so EchoServer0 is null .此外,该对象未初始化,因此EchoServer0null Classes and identifiers have separate namespaces.类和标识符具有单独的命名空间。 This will actually compile:这将实际编译:

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 enum s are all-capitals.请遵守 Java 命名标准:类以大写字母开头,标识符以小写字母开头,常量和enum都是大写字母。

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;

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 Object truly doesn't have such a method, but the type EchoServer0 you define above certainly has.你的Object类型的变量确实没有这样的方法,但是你上面定义的类型EchoServer0肯定有。

Java 方法添加 ArrayList 未定义类型<object><div id="text_translate"><p>我正在为我的作业构建一个 java 程序,我必须将产品添加到特定商店。 尝试从 Store class 添加到 ArrayList 时遇到问题。</p><p> 我有 class 产品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存储如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> - Java Method add ArrayList is undefined for the type <OBJECT>

Java - List 类型的方法 descendingIterator() 未定义<object><div id="text_translate"><p>我的代码是</p><pre>List&lt;Object&gt; listOld = new LinkedList&lt;Object&gt;(); listOld.add("Hello"); listOld.add(1000); listOld.add(25); listOld.add(85.9); Iterator x = listOld.descendingIterator();</pre><p> 错误是:</p><blockquote><p> Tester.java:10: 错误: 找不到符号迭代器 x = listOld.descendingIterator(); ^ 符号:方法 descendingIterator() 位置:List 1 类型的变量 listOld 错误</p></blockquote><p>我怎么解决这个问题?</p></div></object> - Java - The method descendingIterator() is undefined for the type List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的方法没有为这种类型定义? - Why is my method undefined for this type? 此方法是对象的未定义类型 - This method is an undefined type of object 为什么我的方法未定义为 java.lang.String 类型 - Why is my method undefined for type java.lang.String 未为对象类型定义方法window() - The method window() is undefined for the type Object 该类型对象对象的方法未定义 - The method is undefined for the type Object issue 未定义类型对象错误的方法 - The Method is undefined for the type object error 未为类型Object确定方法checkCollision(BoundingShape) - The method checkCollision(BoundingShape) is undefined for the type Object 未为对象类型定义方法setAdapter(SimpleAdapter) - The method setAdapter(SimpleAdapter) is undefined for the type Object Java 方法添加 ArrayList 未定义类型<object><div id="text_translate"><p>我正在为我的作业构建一个 java 程序,我必须将产品添加到特定商店。 尝试从 Store class 添加到 ArrayList 时遇到问题。</p><p> 我有 class 产品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存储如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> - Java Method add ArrayList is undefined for the type <OBJECT> Java - List 类型的方法 descendingIterator() 未定义<object><div id="text_translate"><p>我的代码是</p><pre>List&lt;Object&gt; listOld = new LinkedList&lt;Object&gt;(); listOld.add("Hello"); listOld.add(1000); listOld.add(25); listOld.add(85.9); Iterator x = listOld.descendingIterator();</pre><p> 错误是:</p><blockquote><p> Tester.java:10: 错误: 找不到符号迭代器 x = listOld.descendingIterator(); ^ 符号:方法 descendingIterator() 位置:List 1 类型的变量 listOld 错误</p></blockquote><p>我怎么解决这个问题?</p></div></object> - Java - The method descendingIterator() is undefined for the type List<Object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM