我有以下类能够执行自身的深层复制。
public class XPolygon {
private XVector[] points;
XPolygon(XVector... points)
{
this.points = points;
}
public XPolygon copy()
{
return new XPolygon((XVector[])Arrays.stream(this.points).map(XVector::copy).toArray());
}
}
请将此视为学术活动。调用实例的copy
方法时,会生成以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LXVector;
at XPolygon.copy(XPolygon.java:17)
调用XPolygon
构造函数以防止发生错误的正确方法是什么?