【问题标题】:circle object constructor not defined未定义圆形对象构造函数
【发布时间】:2013-10-16 04:29:12
【问题描述】:

我正在尝试使类 circle 扩展 Shape 类,但不断从 JUnit 收到错误,说构造函数 Circle(Point, int) 未定义我如何定义 Circle 构造函数与 public Circle(Point[] center, int半径)?

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;
}
}

Circle.java

import java.awt.Point;

public class Circle extends Shape{

private double radius;

public Circle(Point[] center, int aradius) {

    if(radius < 0){
        radius = 0;
    }
    else{
    radius = aradius;
    }
    this.setPoints(center);
}

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

}

【问题讨论】:

    标签: java inheritance methods polymorphism


    【解决方案1】:

    只需传递一个Point,而不是数组。

    public Circle(Point center, int aradius)
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2020-09-30
      相关资源
      最近更新 更多