我正在尝试从扩展形状类的 circle.java 类返回一个点。我现在不断收到空指针异常。我需要使用继承的 getPoints() 重新运行中心点;方法,但是 inhereted 方法返回一个数组,并且要从 circle 返回的值不是数组。如果不使用单独的返回方法,我将如何返回中心点。我的形状类如下
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;
}
}