我正在尝试将数组作为参数传递给另一个类,但始终收到错误 “错误:类型不兼容:点不能转换为int []”
我的代码的第一部分是:
public Circle(int n, int x, int y)
{
radius = n;
counter++;
center[0] = x;
center[1] = y;
Point center = new Point(center);
}
Point是需要将数组传递给它的类。
第二部分代码:
public class Point
{
private int xCord;
private int yCord;
public Point (int [] center)
{
xCord = center[0];
yCord = center[1];
答案 0 :(得分:1)
这对我来说尚不清楚,但是显然应该在circle类构造函数中引起错误。
center[0] = x; // center is an int array
center[1] = y;
Point center = new Point(center); // ?????
// ^^^ ^^^^ Duplicate variable names
通过更改新的Point
变量的名称来解决此问题。
答案 1 :(得分:0)
Point center = new Point(center);
变量重复
更改为
Point point = new Point(center);
答案 2 :(得分:-1)
您必须在这里考虑对象。此处的变量名称重复。由于中心对象包含数组引用,因此无法保留相同的变量来保存点对象。
您可以相应地修改代码。随附示例代码
public class Test {
public static void main(String[] args) {
Circle(1,2,3);
}
static void Circle(int n, int x, int y)
{ int center[] = new int[2];
//Do your operation and initialize the array
center[0] = 25;
center[1] = 26;
Point pointObject = new Point(center);
}
}
class Point
{
private int xCord;
private int yCord;
public Point (int [] center)
{
xCord = center[0];
yCord = center[1];
}
}