Java多态/抽象类帮助
我正在尝试为抽象类设置参数:
public abstract class NewMath {
public abstract int op (int intOne, int intTwo);
}
这是扩展子类:
public class MultMath extends NewMath {
public int op (int intOne, int intTwo){
return intOne + intTwo;
}
}
但是当我尝试在定义参数时实例化对象时,如下所示:
public class TestNewMath {
public static void main(String [] _args) {
MultMath multObj = new MultMath(3,5);
}
}
它不起作用。它给了我这个错误:
TestNewMath.java:3: cannot find symbol symbol : constructor AddMath(int,int) location: class AddMath AddMath addObj = new AddMath(3, 5);
我知道我错过了一些东西。它是什么?
I'm trying to set parameters for a abstract class:
public abstract class NewMath {
public abstract int op (int intOne, int intTwo);
}
Here is the extended subclass:
public class MultMath extends NewMath {
public int op (int intOne, int intTwo){
return intOne + intTwo;
}
}
But when I try to instantiate an object while defining the parameters like this:
public class TestNewMath {
public static void main(String [] _args) {
MultMath multObj = new MultMath(3,5);
}
}
It doesn't work. It gives me this error:
TestNewMath.java:3: cannot find symbol symbol : constructor AddMath(int,int) location: class AddMath AddMath addObj = new AddMath(3, 5);
I know I'm missing something. What is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用带有两个 int 参数的构造函数,但尚未创建这样的构造函数。您仅创建了一个名为“op”的方法,该方法采用两个 int 参数。
You're calling a constructor with two int arguments, but you haven't created such a constructor. You have only created a method named 'op' that takes two int arguments.
您可以将构造函数放在“MultMath”类中,如下所示:
这将消除您的编译错误。或者,您可以这样做:
You would put the constructor in the "MultMath" class, like so:
This would get rid of your compile error. Alternatively, you could do this: