编译对象初始化中的参数错误

时间:2017-03-22 22:26:09

标签: 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 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

当我在“max”初始化期间将两种类型定义为参数“int”和“Object”时:

public Element extractMin (){
    Element max = new Element(int i, Object o );
    return max;

我收到错误:

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

导致错误的原因是什么?如何在初始化中正确定义参数?

由于

1 个答案:

答案 0 :(得分:0)

你先分配i和o吗?

int i = 7;
Object o = {[object here]};
public Element extractMin ()
{
    Element max = new Element( i, o );
    return max;
}