0

shape クラスを拡張する circle.java クラスから Point を返そうとしています。現在、ヌルポインタ例外が発生し続けています。継承された getPoints(); を使用して中心点を再実行する必要があります。メソッドですが、継承されたメソッドは配列を返し、circle から返される値は配列ではありません。別の戻りメソッドを作成せずに中心点を返すにはどうすればよいですか。私の Shape クラスは次のとおりです

import java.awt.Point;

public abstract class Shape {
private String  name;
private Point[] points;
protected Shape(){};
protected Shape(String aName) {
    name = aName;
}

public final String getName() {
    // TODO Implement method
    return name;
}

protected final void setPoints(Point[] thePoints) {
    points = thePoints;
}

public final Point[] getPoints() {
    // TODO Implement method
    return points;
}

public abstract double getPerimeter();

public static double getDistance(Point one, Point two) {
    double x = one.getX();
    double y = one.getY();
    double x2 = two.getX();
    double y2 = two.getY();
    double x3 = x - x2;
    double y3 = y - y2;
    double ypow = Math.pow(y3, 2);
    double xpow = Math.pow(x3, 2);
    double added = xpow + ypow;
    double distance = Math.sqrt(added);
    return distance;
}
}

私のサークルクラスは次のとおりです

import java.awt.Point;

public class Circle extends Shape{

private double radius;

public Circle(Point center, int aradius) {
super("Circle");

radius = aradius;
if(radius < 0){
    radius = 0;
}
else{
radius = aradius;
}

}

@Override
public double getPerimeter() {
double perim = 2 * Math.PI * radius;
return perim;
}
  public double getRadius(){
  return radius;
}

}
4

2 に答える 2

1

私が考えることができる最も簡単な解決策は、単に クラスのsetPointsメソッドを使用することです...Shape

public Circle(Point center, int aradius) {
    super("Circle");
    //...
    setPoints(new Point[]{center});
}
于 2013-10-09T02:24:56.823 に答える