gpt4 book ai didi

java - 抽象类需要访问子类属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:22 27 4
gpt4 key购买 nike

我的代码有问题(编辑:这些类的全部代码)

public abstract class SimplePolygon implements Polygon {

//protected Vertex2D[] varray; //this is wrong in tests
public double getWidth(){
double min = varray[0].getX(), max = varray[0].getX();

for(int i = 0;i<varray.length;i++){
max = Math.max(max,varray[i].getX());
min = Math.min(min,varray[i].getX());
}
return max - min;
}

public double getHeight(){
double min = varray[0].getY(), max = varray[0].getY();

for(int i = 0;i<varray.length;i++){
max = Math.max(max,varray[i].getY());
min = Math.min(min,varray[i].getY());
}
return max - min;
}

public double getLength(){
double distance = 0;
for(int i = 0;i<varray.length;i++){
if((i+1)<varray.length){distance += varray[i].distance(varray[i+1]);}
else{distance += varray[i].distance(varray[0]);}
}

return distance;
}

public double getArea(){
double suma = 0;
for(int i = 0;i<varray.length-1;i++){
suma += varray[i].getX()*varray[i+1].getY() - varray[i+1].getX()*varray[i].getY();
}
return suma/2;
}

public String toString(){
String str = "Polygon: vertices =";
for(int i = 0;i<varray.length;i++){
str += " ";
str += varray[i];
}
return str;
}
}

public class ArrayPolygon extends SimplePolygon {

public ArrayPolygon(Vertex2D[] array){
varray = new Vertex2D[array.length];
if (array == null){}
for(int i = 0;i<array.length;i++){
if (array[i] == null){}
varray[i] = array[i];
}
}

public Vertex2D getVertex(int index) throws IllegalArgumentException{
return varray[index];
}

public int getNumVertices(){
return varray.length;
}

}

问题是,我不允许向抽象类 SimplePolygon 添加任何属性或方法,因此我无法正确初始化 varray。它可以简单地用那个类中的 protected attrib 来解决,但是由于某些(愚蠢的)原因我不能那样做。有没有人知道如何在没有它的情况下解决它?感谢所有帮助。

最佳答案

想想:

  • 你的 Polygon 接口(interface)作为 java.util.List 接口(interface)
  • 您的 SimplePoygon 抽象类作为 java.util.AbstractCollection
  • 您的 ArrayPolygon 具体类为 java.util.ArrayList

我认为你的任务的重点是用迭代器实现一个解决方案,这样你就可以在抽象类中实现关于多边形的通用方法,同时在具体类中隐藏包含数据点的实际数据结构;所以:

/* 
* This class implements generic methods about polygon, like size, area and
* so on, leveraging on the Vertex2D iterator.
*/
public abstract class SimplePolygon implements Polygon {

// all concrete subclasses must implement iterator
public abstract Iterator<Vertex2D> iterator();

// this generic method prints the vertex list
// using iterator hides the data structure used in implementation
public String toString() {
Iterator<Vertex2D> it = iterator();
if (! it.hasNext())
return "[]";

StringBuilder sb = new StringBuilder();
sb.append('Polygon: vertices = [');
for (;;) {
Vertex2D e = it.next();
if (! it.hasNext())
return sb.append("]").toString();
sb.append(' ');
}
}
}

// for instance this concrete classes uses an ArrayList
public ArrayPolygon extends SimplePolygon {

public Iterator<Vertex2D> iterator() {
return new .....
}
}

关于java - 抽象类需要访问子类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13241618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com