必须实现继承的抽象方法错误

发布于 2025-02-09 01:25:32 字数 1719 浏览 1 评论 0原文

我遇到了sphere.java的错误,

The type sphere must implement the inherited abstract method GeometricObjects.hit(Ray)

虽然我看不到我如何获得此错误,因为我定义了命中方法,如下所示。

以下是sphere.java代码:

package Objects;
import Utility.*;

public class Sphere extends GeometricObjects {

    Point3d center;
    double radius;

    public Sphere(Point3d center, double radius, Color color) {
        this.center = new Point3d(center);
        this.radius = radius;
        this.color = new Color(color);
    }

    public double hit(Ray ray) {
        double a = ray.direction.dot(ray.direction);
        double b = 2*ray.origin.sub(center).dot(ray.direction);
        double c = ray.origin.sub(center).dot(ray.origin.sub(center)) - radius*radius;

        double discriminant = b*b - 4*a*c;

        if (discriminant < 0.0){
            return 0.0;
        }
        else {
            double t = (-b - Math.sqrt(discriminant)) / (2*a);

            if (t > 10E-9) {
                return t;
            }
            else {
                return 0.0;
            }
        }
    }
}

这是geomegobobjects.java代码:

package Objects;
import Utility.*;

public abstract class GeometricObjects {
    public Color color;

    public abstract double hit(Ray ray);
}

和Heres ray.java代码>代码:

package Utility;

public class Ray {
    public Point3d origin;
    public Vector direction;

    public Ray(Point3d origin, Vector direction) {
        this.origin = new Point3d(origin);
        this.direction = new Vector(direction);
    }
}

I'm getting an error for Sphere.java that says

The type sphere must implement the inherited abstract method GeometricObjects.hit(Ray)

Though I don't see how I am getting this error as I've defined the hit method as shown below.

Below is the Sphere.java code:

package Objects;
import Utility.*;

public class Sphere extends GeometricObjects {

    Point3d center;
    double radius;

    public Sphere(Point3d center, double radius, Color color) {
        this.center = new Point3d(center);
        this.radius = radius;
        this.color = new Color(color);
    }

    public double hit(Ray ray) {
        double a = ray.direction.dot(ray.direction);
        double b = 2*ray.origin.sub(center).dot(ray.direction);
        double c = ray.origin.sub(center).dot(ray.origin.sub(center)) - radius*radius;

        double discriminant = b*b - 4*a*c;

        if (discriminant < 0.0){
            return 0.0;
        }
        else {
            double t = (-b - Math.sqrt(discriminant)) / (2*a);

            if (t > 10E-9) {
                return t;
            }
            else {
                return 0.0;
            }
        }
    }
}

This is the GeometricObjects.java code:

package Objects;
import Utility.*;

public abstract class GeometricObjects {
    public Color color;

    public abstract double hit(Ray ray);
}

And heres the Ray.java code:

package Utility;

public class Ray {
    public Point3d origin;
    public Vector direction;

    public Ray(Point3d origin, Vector direction) {
        this.origin = new Point3d(origin);
        this.direction = new Vector(direction);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文