英文:
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'
。
我有两个选择:
- 声明为抽象类,这对我来说不可行。
- 在
ActionCalculateQuadrangle
中实现抽象方法getLengthLine(Line)
。这意味着我必须实现Line
类中的所有抽象方法。
在更新后仍然出现错误:
英文:
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<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);
}
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 'CalculateQuadrangle' must either be declared abstract or implement abstract method 'getLengthLine(Line)' in 'ActionCalculateQuadrangle'
.
- Declarate abstract - can't for me.
- implement abstract method 'getLengthLine(Line)' in 'ActionCalculateQuadrangle'. That mean, what i must implement ALL abstract method, which have in the argument object class Line.
How correct to make (move) the class 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论