当我正确查看未定义的方法(构造函数)时,出现有关未定义方法(构造函数)的错误? (爪哇)
我在这里收到一个错误,说我还没有定义方法,但它在代码中是正确的。
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 ^^^^^^
}
我确实为子类定义了构造函数,为什么当我尝试在 return 语句中传递它时却说我没有?
I'm getting an error here that says I haven't defined a method, but it is right in the code.
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 ^^^^^^
}
I did define that Constructor for SubClass, why is it saying that I didn't when I try to pass it in that return statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要
new SubClass(c, x)
而不是SubClass(c, x)
。在java中,你调用构造函数的方式与方法不同:使用new
关键字。有关该主题的更多信息。
You probably want
new SubClass(c, x)
instead ofSubClass(c, x)
. In java you invoke constructor in different way than method: withnew
keyword.More on the subject.
我想你希望它是:
I think you want it to be:
正如其他人正确指出的那样,缺少调用构造函数所需的
new
。您的情况发生的情况是,由于缺少
new
您的调用被视为方法调用,并且在您的类中没有方法 SubClass(c, x)。在您的情况下,错误未定义的方法是正确的,因为没有名为SubClass(c, x)
的方法您需要更正相同的内容:
As rightly pointed out by others there is missing
new
required to invoke constructor.Whats happening in your case here is that due to missing
new
your call is treated as method call, And in your class there is not method SubClass(c, x). And Error undefined method is correct in your case as there is no method namedSubClass(c, x)
You need to correct same :