java 构造函数 ... 不适用(实际和形式参数列表的长度不同) - 但它确实匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15149822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Constructor ... is not applicable (actual and formal argument lists differ in length) - but it does match
提问by MicroMumbler
So i'm doing an assignment for school in java... It's a class hierarchy kind of assignment and we are supposed to make a "Triangle.java" class that extends a "ClosedShape.java" class which extends a "Shape.java" .... the ClosedShape and Shape are both given to us, so most likely nothing wrong with them (i'll post them anyway) but my Triangle class is as follows:
所以我正在用 java 为学校做一个作业......这是一种类层次结构的作业,我们应该创建一个“Triangle.java”类来扩展一个“ClosedShape.java”类,它扩展一个“Shape.java” " .... ClosedShape 和 Shape 都是给我们的,所以它们很可能没有问题(无论如何我都会发布它们)但是我的 Triangle 类如下:
public abstract class Triangle extends ClosedShape{
public Triangle(int[] a, int[] b, int base, int height){
super(true, 3, a, b);
setWidth(base);
setHeight(height);
setXYCoords(a, b);
}
public Triangle(int x, int y, int base, int height){
int[] a = new int[3];
int[] b = new int[3];
a[0] = x;
a[1] = (x+base)/2;
a[2] = (x+base);
b[0] = y;
b[1] = (y+height)/2;
b[2] = (y+height);
}
}
The reason I have two constructors is because i need to create those two arrays to hold the points for drawing the shape...and then I need to have them passed into the ClosedShape(boolean, int, int[], int[]) super class... if i create the arrays in the same constructor they would need to be defined before the call to super() so they could be passed in, but that's not allowed because the call to super() must be first...so currently, when I try to compile Triangle.java i get the error:
我有两个构造函数的原因是因为我需要创建这两个数组来保存绘制形状的点……然后我需要将它们传递到 ClosedShape(boolean, int, int[], int[])超类...如果我在同一个构造函数中创建数组,则需要在调用 super() 之前定义它们,以便可以传入它们,但这是不允许的,因为必须首先调用 super() 。 .so 目前,当我尝试编译 Triangle.java 时出现错误:
Triangle.java.14: error: no suitable constructor found for ClosedShape()
{ //little arrow pointing under the '{'
constructor ClosedShape.ClosedShape(boolean, int, int[], int[]) is not applicable
(actual and formal argument lists differ in length)
constructor ClosedShape.ClosedShape(boolean, int) is not applicable
(actual and formal argument lists differ in length)
1 error
It also specifies in the assignment that the signature for the triangle mustbe Traingle(int x, int y, int base, int height)... So....i'm confused because if i'm not mistaken (which java believes i am...) I made a super call with all the proper values, and there isa constructor "ClosedShape(boolean, int, int[], int[])"... heres the ClosedShape class:
它还在赋值中指定三角形的签名必须是 Traingle(int x, int y, int base, int height)...所以....我很困惑,因为如果我没有弄错(哪个 java相信我是...)我做了一个超级调用所有的正确的价值观,有是一个构造函数“ClosedShape(布尔,INT,INT [],INT [])” ...继承人的ClosedShape类:
import java.awt.Graphics;
public abstract class ClosedShape extends Shape {
boolean polygon;
int numPoints;
int[] xVertices;
int[] yVertices;
int x,y,width, height;
public ClosedShape(boolean isPolygon, int numPoints) {
super(0,0);
this.polygon = isPolygon;
this.numPoints = numPoints;
}
public ClosedShape(boolean isPolygon, int numPoints, int[] x, int[] y) {
super(x[0],y[0]);
this.polygon = isPolygon;
if (isPolygon) {
this.numPoints = numPoints;
xVertices = new int[numPoints]; // error check? if x.length == numPoints
//for (int i = 0; i < x.length; i++) { // make copy of array: why?
// xVertices[i] = x[i];
//}
yVertices = new int[numPoints]; // error check? if y.length == numPoints
for (int i = 0; i < y.length; i++) { // make copy of array
yVertices[i] = y[i];
}
}
else { // its an oval - define bounding box
this.numPoints = 4;
this.x = x[0];
this.y = y[0];
width = x[1];
height = y[1];
}
}
public void setXYCoords(int[] x, int[] y){
this.xVertices = x;
this.yVertices = y;
}
// Gives access to the width attribute
public void setWidth(int width){
this.width = width;
}
// Gives access to the height attribute
public void setHeight(int height) {
this.height = height;
}
public void draw(Graphics g) {
if (polygon) {
g.drawPolygon(xVertices, yVertices, numPoints);
}
else {
g.drawOval(x, y, width, height);
}
}
public abstract double Area();
public abstract double Perimeter();
}
采纳答案by MicroMumbler
I finally figured it out. I'm making a call to a constructor that I don't even need to use! I only need to make a call to the first constructor then use setXYCoords()
method to do what i need to do with the arrays.... heres my final code:
我终于弄明白了。我正在调用一个我什至不需要使用的构造函数!我只需要调用第一个构造函数然后使用setXYCoords()
方法来做我需要对数组做的事情......这是我的最终代码:
(ClosedShape.java stayed the same)
(ClosedShape.java 保持不变)
import java.awt.Graphics;
public class Triangle extends ClosedShape{
public Triangle(int x, int y, int base, int height){
super(true, 3);
setWidth(base);
setHeight(height);
int [] arrayX = new int[3];
arrayX[0] = x;
arrayX[1] = (x+(width/2));
arrayX[2] = (x+width);
int [] arrayY = new int[3];
arrayY[0] = y;
arrayY[1] = (y-height);
arrayY[2] = y;
setXYCoords(arrayX, arrayY);
}
public double Area(){
return 0;
}
public double Perimeter(){
return 0;
}
}
回答by duffymo
The problem is that there's no default, no-arg constructor for ClosedShape
.
问题是没有默认的、无参数的构造函数ClosedShape
。
Look at this ctor:
看看这个演员:
public Triangle(int x, int y, int base, int height){
There's no explicit call to a super()
constructor, so the compiler assumes that it needs to call the no-arg constructor. But there isn't one...
没有显式调用super()
构造函数,因此编译器假定它需要调用无参数构造函数。但是没有一个...
回答by Aurand
As @duffymo has stated, if you do not explicitly call super()
, the compiler will insert a call to the no-arg constructor.
正如@duffymo 所说,如果您不显式调用super()
,编译器将插入对无参数构造函数的调用。
I think the solution you're looking for is a Factory Method. You can create a static method, createTriangle(int x, int y, int base, int height)
, for example. Build your arrays in that method, call the appropriate constructor and then return the constructed object.
我认为您正在寻找的解决方案是工厂方法。例如,您可以创建一个静态方法createTriangle(int x, int y, int base, int height)
。在该方法中构建数组,调用适当的构造函数,然后返回构造的对象。