Java 当I';我正看着它?(爪哇)

Java 当I';我正看着它?(爪哇),java,constructor,undefined,Java,Constructor,Undefined,我在这里得到一个错误,说我没有定义一个方法,但它在代码中是正确的 class SubClass<E> extends ExampleClass<E>{ Comparator<? super E> c; E x0; SubClass (Comparator<? super E> b, E y){ this.c = b; this.x0 = y; }

我在这里得到一个错误,说我没有定义一个方法,但它在代码中是正确的

class SubClass<E> extends ExampleClass<E>{
      Comparator<? super E> c;
      E x0;

      SubClass (Comparator<? super E> b, E y){
           this.c = b;
           this.x0 = y;
      }

      ExampleClass<E> addMethod(E x){
           if(c.compare(x, x0) < 0)
               return new OtherSubClassExtendsExampleClass(c, x0, SubClass(c, x)); 
               //this is where I get the error                        ^^^^^^
      }
class子类扩展了ExampleClass{

Comparator您可能想要
新的子类(c,x)
而不是
子类(c,x)
。在java中,调用构造函数的方式与调用方法不同:使用
new
关键字


.

我想你希望它是:

                                       // new is needed to invoke a constructor 
return new OtherSubClassExtendsExampleClass(c, x0, new SubClass(c, x));

正如其他人正确指出的那样,调用构造函数需要缺少
new

在您的案例中发生的是,由于缺少
new
,您的调用被视为方法调用,并且在您的类中没有方法子类(c,x)。错误未定义的方法在您的案例中是正确的,因为没有名为
子类(c,x)

您需要纠正相同的错误:

return new OtherSubClassExtendsExampleClass(c, x0, new SubClass(c, x));