为什么我得到以下错误,我应该在空构造函数中有什么?

时间:2015-03-22 03:22:16

标签: java inheritance constructor compiler-errors abstract-class

我有一个抽象类,TwoDPolygon。

abstract class TwoDPolygon
{
double width, height;
String name;

public TwoDPolygon()
{
width = 0;
height = 0;
name = null;
}

public TwoDPolygon(double width, double height, String name)
{
this.width = width;
this. height = height;
this.name = name;
}

public TwoDPolygon(double equalWidthHeight, String name)
{
width = equalWidthHeight;
height = equalWidthHeight;
name = name;
}

public String getName()
{
return name;
} 

abstract double area();
}

扩展TwoDPolygon的子类Triangle。

public class Triangle extends TwoDPolygon
{

String status;

public Triangle()
{
super();
}

public Triangle(String status, double width, double height)
{
this.status = status;
this.width = width;
this.height = height;
}

public Triangle(double size)
{

}

public String getStatus()
{
if ("filled".equals(status))
status = "filled";
if ("notFilled".equals(status))
status = "notFilled";
return status;
}

double area()
{
return (1/2)*(width)*(height); 
}
}

这是一个提供给我的主要方法。给出了主要方法和一些说明,我自己写的另外两个类。

public class Ex2Driver
{

public static void main(String[] args)
{
TwoDPolygon polygons[] = new TwoDPolygon[3];
polygons[0] = new Triangle("filled", 8.5, 12.0);
polygons[1] = new Triangle("not Filled", 6.5, 7.5);
polygons[2] = new Triangle(7.0);

for (int i=0; i<polygons.length; i++)
{
System.out.println("Object is " + polygons[i].getName());
System.out.println("Triangle " + polygons[i].getStatus());
System.out.println("Area is " + polygons[i].area());
}
}
}

我意识到Triangle类中的第三个构造函数是空的。我知道它应该在那里,从指示,但我坚持在里面放什么。无论如何,我从驱动程序类行

得到“找不到符号”错误
System.out.println("Triangle " + polygons[i].getStatus());

3 个答案:

答案 0 :(得分:1)

问题是你没有在所有构造函数中调用super。你必须这样做。

而不是:

public Triangle(String status, double width, double height)
{
this.status = status;
this.width = width;
this.height = height;
}

public Triangle(double size)
{

}

这样做:

public Triangle(String status, double width, double height)
{
super(width, height, status);
this.status = status;
this.width = width;
this.height = height;
}

public Triangle(double size)
{
super(size, null);
}

编辑#1

此外,数组只携带属于它的类的方法,即使你添加一个共享该类的对象,你也无法访问子类中的方法。 你应该使用arraylists,即使超类没有它们,也会使用这些方法。所以看起来应该是这样的:

ArrayList<TwoDPolygons> polygons = new ArrayList<TwoDPolygon>(3);
polygons.add(new Triangle("filled", 8.5, 12.0));
polygons.add(new Triangle("not Filled", 6.5, 7.5));
polygons.add(new Triangle(7.0));

for (int i=0; i<polygons.size(); i++)
{
System.out.println("Object is " + polygons.get(i).getName());
System.out.println("Triangle " + polygons.get(i).getStatus());
System.out.println("Area is " + polygons.get(i).area());
}

编辑#2 如果你不能修改Ex2Driver,那么在你的TwoDPolygon类中添加一个getStatus的抽象方法,如abstract String getStatus();,这应该可以解决调用方法的问题。

现在为了获取名称的null,这是因为在Triangle构造函数中,当我放置超级调用时我将null设置为名称,您可以将其更改为&#34; Triangle&#34;像这样:

public Triangle(String status, double width, double height)
{
super(width, height, "Triangle");
this.status = status;
this.width = width;
this.height = height;
}

public Triangle(double size)
{
super(size, "Triangle");
}

同样在TwoDPolygon(double equalWidthHeight, String name)构建者name = name;中,只需将临时变量名称设置为等于自己,您应该this.name = name

确保在每个构造函数中设置名称,以便在运行时不会为null。在三角形构造函数中,您执行name = "Triangle";,但在TwoDPolygon构造函数中执行name = "TwoDPolygon";,所以如果您创建了更多的子类&#39;你可以看到你是否忘记设置名称,它看起来更好。

在您的区域方法中,请务必执行return (0.5 * width * height);来解决获取0.0的问题。

在所有没有参数状态的Triangle构造函数中,请务必执行status = "filled";status = "notFilled";,以免在答案中获得null

希望这有帮助:D

答案 1 :(得分:1)

因为多边形[]的类型是TwoDPolygon TwoDPolygon polygons[] = new TwoDPolygon[3];,所以它会在TwoDPolygon的类声明中搜索方法getStatus(),而不是Triangle,即使这些元素被实例化为Triangle。

您有几种方法可以解决此问题:

  1. 将声明更改为Triangle

    Triangle polygons[] = new Triangle[3];

  2. 将getStatus方法移至TwoDPolygon

  3. 在每个循环中将对象强制转换为Triangle

    System.out.println("Triangle " + ((Triangle)polygons[i]).getStatus());

答案 2 :(得分:0)

您将阵列多边形初始化为

TwoDPolygon polygons[] = new TwoDPolygon[3];

然而,当您从数组中访问元素时,您正在调用子类Triangle中的方法。您不能从TwoDPolygon调用仅三角形方法。

将数组初始化为Triangle元素或将getStatus()移动到类TwoDPolygon。