[英]Constructor cannot be applied to given types
可能是一个简单的问题,我只是不太了解,所以一直将我的头靠在墙上。 我有这三个类,试图在它们之间传递对象/方法。
继承人头等舱
public class LargeMapDriver
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int value1;
ThyPoint p1 = new ThyPoint(132, 734);
ThyPoint p2 = new ThyPoint(56, 998);
ThyPoint p3 = new ThyPoint(100, 105);
System.out.println("Enter value: ");
order = keyboard.nextInt();
LargeMap myMap = new LargeMap(value1, p1, p2, p3);
其次,指针类。
public class ThyPoint
{
private int a;
private int b;
//constructor
public ThyPoint(int x, int y)
{
a = x;
b = y;
}
//...
//set and get methods for a and b... not shown
//...
public String toString()
{
return "a: " + getValueA() + " b: " + getValueA();
}
}
最后一类,显示构造函数错误。
public class LargeMap
{
//GETTING CONSTRUCTOR(s) ERROR
public static void goodMethod(int value1, ThyPoint p1, ThyPoint p2, ThyPoint p3)
{
if ( value1 == 0 )
System.out.println( p1.toString() + p2.toString() + p3.toString());
else
System.out.println( p2.toString() + p3.toString() + p1.toString());
}
}
好的,所以会出现问题:
**constructor LargeMap in class LargeMap cannot be applied to given types;
LargeMap myMap = new LargeMap(value1, p1, p2, p3);
^
required: no arguments
found int,ThyPoint,ThyPoint,ThyPoint
reason: actual and formal arguments differ in length**
因此,我试图为LargeMap类创建一个构造函数,但是失败了,我试图将p1,p2,p3对象中的那些值传递给构造函数以接受。 并初始化它们中的值,我该怎么做? 我要在其中初始化的值是:
ThyPoint p1 = new ThyPoint(132, 734);
ThyPoint p2 = new ThyPoint(56, 998);
ThyPoint p3 = new ThyPoint(100, 105);
而且,LargeMap类必须保持为空。 它不必是静态的或公共的。
LargeMap类没有构造函数,该构造函数根本不接受四个参数。 在没有构造函数的情况下,将只有不带参数的默认构造函数,它是由Java本身加起来的。
也许为构造函数发布代码可能会有所帮助。
尝试
public LargeMap (int value1, ThyPoint p1, ThyPoint p2, ThyPoint p3) {
//do whatever you want
}
您正在尝试使用未在“ LargeMap”类中声明的构造函数
这样声明构造函数,而不是在“ LargeMap”类中声明goodMethod()方法
public LargeMap(int value1, ThyPoint p1, ThyPoint p2, ThyPoint p3)
{
if ( value1 == 0 )
System.out.println( p1.toString() + p2.toString() + p3.toString());
else
System.out.println( p2.toString() + p3.toString() + p1.toString());
}
在您的情况下,您只能访问Java编译器提供的默认构造函数(无参数构造函数)
因此,像上面那样编写自己的构造函数。
或者您可以直接调用静态方法
LargeMap.goodMethod(value1, p1, p2, p3);
而不是去创建对象。即代替代码中的以下行
LargeMap myMap = new LargeMap(value1, p1, p2, p3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.