如何在Java中将类的访问权限设置为internal?

huangapple go评论88阅读模式
英文:

How correct to make (move) the class internal in Java?

问题

项目的结构如下:

/src/
----/action/
--------/impl/
------------ActionCalculateQuadrangle.interface
--------CalculateQuadrangle.class
--------Line.class
----/other packages/

ActionCalculateQuadrangle.interface 代码部分:

public interface ActionCalculateQuadrangle {
    double getArea(Quadrangle quadrangle);
    double getPerimeter(Quadrangle quadrangle);
    double getLengthLine(Line line);
    double getSlope(Line line);

    List<Point[]> getCombinationsElementsOver(Point[] points, int pointsOnOneLine);
    Point getCrossPoint(Line line1, Line line2);

    boolean isQuadrangle(Quadrangle quadrangle);
    boolean isPointsOnStraightLine(Point[] points);
    boolean isConvex(Quadrangle quadrangle);
    boolean isSquare(Quadrangle quadrangle);
    boolean isRhombus(Quadrangle quadrangle);
    boolean isTrapezoid(Quadrangle quadrangle);
    boolean isDeltoid(Quadrangle quadrangle);
    boolean isStraightPerpendicular(Line line1, Line line2);
    boolean isEqualLengthsDiagonals(Line line1, Line line2);
    boolean isCrossLines(Line line1, Line line2);
    boolean areSidesParallel(Line side1, Line side2);
}

Line.class 代码部分:

public class Line {
    private final Point p1;
    private final Point p2;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getP1() {
        return this.p1;
    }

    public Point getP2() {
        return this.p2;
    }
}

部分 CalculateQuadrangle.class 代码(为了不占用太多空间,我引用了部分代码):

// ... 一些代码

@Override
public double getPerimeter(final Quadrangle quadrangle) {
    Line side1 = new Line(quadrangle.getP1(), quadrangle.getP2());
    Line side2 = new Line(quadrangle.getP2(), quadrangle.getP3());
    Line side3 = new Line(quadrangle.getP3(), quadrangle.getP4());
    Line side4 = new Line(quadrangle.getP1(), quadrangle.getP1());

    return getLengthLine(side1) + getLengthLine(side2) + getLengthLine(side3) + getLengthLine(side4);
}

@Override
public double getLengthLine(final Line line) {
    Point p1 = line.getP1();
    Point p2 = line.getP2();

    return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
}

@Override
public double getSlope(final Line line) {
    Point p1 = line.getP1();
    Point p2 = line.getP2();

    if ((p1.getY() == p2.getY()) || (p1.getX() == p2.getX())) {
        return 0;
    } else {
        return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
    }
}

// ... 一些代码

现在我想要将 Line 类变成内部类。我应该怎么做?像这样:

// ... 一些代码

public class CalculateQuadrangle {
    // ... 其他代码

    public class Line {
        private final Point p1;
        private final Point p2;

        public Line(Point p1, Point p2) {
            this.p1 = p1;
            this.p2 = p2;
        }

        public Point getP1() {
            return this.p1;
        }

        public Point getP2() {
            return this.p2;
        }
    }

    // ... 其他代码
}

// ... 一些代码

但是 Java 编译器显示错误消息:Class 'CalculateQuadrangle' must either be declared abstract or implement abstract method 'getLengthLine(Line)' in 'ActionCalculateQuadrangle'

我有两个选择:

  1. 声明为抽象类,这对我来说不可行。
  2. ActionCalculateQuadrangle 中实现抽象方法 getLengthLine(Line)。这意味着我必须实现 Line 类中的所有抽象方法。

在更新后仍然出现错误:

如何在Java中将类的访问权限设置为internal?
如何在Java中将类的访问权限设置为internal?
如何在Java中将类的访问权限设置为internal?

英文:

Structure of my project is:

/src/
----/action/
--------/impl/
------------ActionCalculateQuadrangle.interface
--------CalculateQuadrangle.class
--------Line.class
----/other packages/

Code ActionCalculateQuadrangle.interface:

public interface ActionCalculateQuadrangle {
    double getArea(Quadrangle quadrangle);
    double getPerimeter(Quadrangle quadrangle);
    double getLengthLine(Line line);
    double getSlope(Line line);

    List&lt;Point[]&gt; getCombinationsElementsOver(Point[] points, int pointsOnOneLine);
    Point getCrossPoint(Line line1, Line line2);

    boolean isQuadrangle(Quadrangle quadrangle);
    boolean isPointsOnStraightLine(Point[] points);
    boolean isConvex(Quadrangle quadrangle);
    boolean isSquare(Quadrangle quadrangle);
    boolean isRhombus(Quadrangle quadrangle);
    boolean isTrapezoid(Quadrangle quadrangle);
    boolean isDeltoid(Quadrangle quadrangle);
    boolean isStraightPerpendicular(Line line1, Line line2);
    boolean isEqualLengthsDiagonals(Line line1, Line line2);
    boolean isCrossLines(Line line1, Line line2);
    boolean areSidesParallel(Line side1, Line side2);
}

Code Line.class:

public class Line {
    private final Point p1;
    private final Point p2;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getP1() {
        return this.p1;
    }

    public Point getP2() {
        return this.p2;
    }
}

Part of code unity-class 'CalculateQuadrangle.class` (I cite part of the code so as not to clutter up the place):

// ... some code

@Override
    public double getPerimeter(final Quadrangle quadrangle) {
        Line side1 = new Line(quadrangle.getP1(), quadrangle.getP2());
        Line side2 = new Line(quadrangle.getP2(), quadrangle.getP3());
        Line side3 = new Line(quadrangle.getP3(), quadrangle.getP4());
        Line side4 = new Line(quadrangle.getP1(), quadrangle.getP1());

        return getLengthLine(side1) + getLengthLine(side2) + getLengthLine(side3) + getLengthLine(side4);
    }

    @Override
    public double getLengthLine(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
    }

    @Override
    public double getSlope(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        if ((p1.getY() == p2.getY()) || (p1.getX() == p2.getX())) {
            return 0;
        } else {
            return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
        }
    }

// ... some code

That's ok. But A want to make class 'Line' internal.

How I do it? Like this:

// ... some code

// PUT IT HERE!
public class Line {
    private final Point p1;
    private final Point p2;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getP1() {
        return this.p1;
    }

    public Point getP2() {
        return this.p2;
    }
}

@Override
    public double getPerimeter(final Quadrangle quadrangle) {
        Line side1 = new Line(quadrangle.getP1(), quadrangle.getP2());
        Line side2 = new Line(quadrangle.getP2(), quadrangle.getP3());
        Line side3 = new Line(quadrangle.getP3(), quadrangle.getP4());
        Line side4 = new Line(quadrangle.getP1(), quadrangle.getP1());

        return getLengthLine(side1) + getLengthLine(side2) + getLengthLine(side3) + getLengthLine(side4);
    }

    @Override
    public double getLengthLine(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
    }

    @Override
    public double getSlope(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        if ((p1.getY() == p2.getY()) || (p1.getX() == p2.getX())) {
            return 0;
        } else {
            return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
        }
    }

// ... some code

But compiler java show me error like this - Class &#39;CalculateQuadrangle&#39; must either be declared abstract or implement abstract method &#39;getLengthLine(Line)&#39; in &#39;ActionCalculateQuadrangle&#39;.

  1. Declarate abstract - can't for me.
  2. implement abstract method 'getLengthLine(Line)' in 'ActionCalculateQuadrangle'. That mean, what i must implement ALL abstract method, which have in the argument object class Line.

如何在Java中将类的访问权限设置为internal?

How correct to make (move) the class internal?

After update still have error:
如何在Java中将类的访问权限设置为internal?
如何在Java中将类的访问权限设置为internal?

答案1

得分: 1

你看到的错误是因为现在有两个Line类,而方法定义和接口定义都没有引用到现在的CalculateQuadrangle.Line类。

英文:

The error you are seeing is because you now have two Line classes, and the method definition and interface definition are not referring to now the same class of CalculateQuadrangle.Line

huangapple
  • 本文由 发表于 2020年8月26日 04:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63586531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定