Java 对象初始化中参数的编译错误
我有一个类元素的对象:Java 对象初始化中参数的编译错误,java,types,constructor,arguments,Java,Types,Constructor,Arguments,我有一个类元素的对象: public class Element { public int key; public Object data; public Element(int i, Object o){ this.key = i; this.data = o; } } 当我使用: public Element extractMin (){ Element max = new Element( i, o ); return
public class Element {
public int key;
public Object data;
public Element(int i, Object o){
this.key = i;
this.data = o;
}
}
当我使用:
public Element extractMin (){
Element max = new Element( i, o );
return max;
我得到一个错误:
PQHeap.java:46: error: cannot find symbol
Element max = new Element( i, o );
^
symbol: variable i
location: class PQHeap
PQHeap.java:46: error: cannot find symbol
Element max = new Element( i, o );
^
symbol: variable o
location: class PQHeap
2 errors
PQHeap.java:46: error: '.class' expected
Element max = new Element( int i, Object o );
^
PQHeap.java:46: error: ';' expected
Element max = new Element( int i, Object o );
^
PQHeap.java:46: error: not a statement
Element max = new Element( int i, Object o );
^
PQHeap.java:46: error: ';' expected
Element max = new Element( int i, Object o );
当我在“max”初始化期间将这两种类型定义为参数“int”和“Object”时:
public Element extractMin (){
Element max = new Element(int i, Object o );
return max;
我得到一个错误:
PQHeap.java:46: error: cannot find symbol
Element max = new Element( i, o );
^
symbol: variable i
location: class PQHeap
PQHeap.java:46: error: cannot find symbol
Element max = new Element( i, o );
^
symbol: variable o
location: class PQHeap
2 errors
PQHeap.java:46: error: '.class' expected
Element max = new Element( int i, Object o );
^
PQHeap.java:46: error: ';' expected
Element max = new Element( int i, Object o );
^
PQHeap.java:46: error: not a statement
Element max = new Element( int i, Object o );
^
PQHeap.java:46: error: ';' expected
Element max = new Element( int i, Object o );
错误的原因是什么?如何在初始化中正确定义参数
谢谢您是否先分配了i和o
int i = 7;
Object o = {[object here]};
public Element extractMin ()
{
Element max = new Element( i, o );
return max;
}
从方法调用中删除
int
和Object
,并将它们声明为变量(或参数或字段)。在将它们用作参数之前,需要定义并初始化i
和o
<如果i
和o
不存在,则code>新元素(i,o)没有任何意义。此外,它们不必被称为i
和o
,因为它们在参数定义中被称为i
和o
。Andy说了什么,另外,你认为i
和o
来自哪里?这些变量不在您的范围内。。。请不要命名变量i
和o
…当我删除类型时,会出现上面显示的错误1。我想I和o已经在上面的类中初始化了。如果我错了,请纠正我。我知道它们不必叫同一个名字,我使用这些名字是为了方便。命名它们不会真正改变任何东西,但由于澄清了错误,2通过另一个类中定义的输入方法将值分配给初始化的i和o,该类获取输入值i和o,并调用PQHeap.extractMin()初始化类型/类元素的“max”对象。这可能会引起问题吗?你说得对,特蕾西:我没有为“max”对象定义值。输入值转到同一类元素的另一个对象,但调用时未指定“max”。谢谢