Java 错误:can';即使是公共变量也找不到符号?
我得到了很多错误,比如下面的一个,我很困惑为什么我没有直接访问这个私有变量Java 错误:can';即使是公共变量也找不到符号?,java,Java,我得到了很多错误,比如下面的一个,我很困惑为什么我没有直接访问这个私有变量 symbol: method GetP() location: class LineSegment FastCollinearPoints.java:47: error: cannot find symbol Point big = nonFinishedSegments.get(i).GetQ() 下面是声明未找到符号的相关类 public class LineSegment { p
symbol: method GetP()
location: class LineSegment
FastCollinearPoints.java:47: error: cannot find symbol
Point big = nonFinishedSegments.get(i).GetQ()
下面是声明未找到符号的相关类
public class LineSegment {
private Point p; // one endpoint of this line segment
private Point q; // the other endpoint of this line segment
public Point GetQ(){
return this.q;
}
public Point GetP(){
return this.p;
}
public LineSegment(Point p, Point q) {
if (p == null || q == null) {
throw new NullPointerException("argument is null");
}
this.p = p;
this.q = q;
}
下面是另一个脚本中发现错误的部分(所有GetQ和GetP都会绘制一个错误)
公共耳穴(点[]点){
final ArrayList nonFinishedSegments=new ArrayList();
if(检查空点(点)| |检查重复(点)){
抛出新的IllegalArgumentException(“异常”);
}
Point[]copy=points.clone();
数组。排序(复制);
对于(int i=0;i
您的代码看起来不错,但我确实看到您没有为FastCollinearPoints方法添加返回类型。如果您是有意添加的,请尝试刷新/重建项目。此外,我建议遵循方法的命名约定,例如,GetP()应为GetP()。我仍然会遇到错误,我已经编译了多次,但它仍然不起作用,但感谢您提供了有关命名约定的提示。@gagarwa--如果代码不编译,调试器会有帮助吗?OP--您是如何构建应用程序的?当存在旧版本的编译类时,有时会出现这种奇怪的编译时错误潜伏在构建环境中。您需要提供一个解决方案,因为无法根据您提供的代码确定问题。(一种可能是您在FastCollinearPoints
中使用的LineSegment
类与您向我们展示的类不同。请检查导入、项目配置和您的构建方法。)@Kevin Boone您明白了。我只关注代码,忽略了这一要点!
public FastCollinearPoints(Point[] points) {
final ArrayList<LineSegment> nonFinishedSegments = new ArrayList<LineSegment>();
if (checkForNullPoints(points) || checkForDuplicates(points)) {
throw new IllegalArgumentException("exception");
}
Point[] copy = points.clone();
Arrays.sort(copy);
for(int i = 0; i < copy.length-1; i++){
nonFinishedSegments.add( new LineSegment(copy[i],copy[i+1]));
}
for(int i = 0; i < nonFinishedSegments.size(); i ++){
final ArrayList<LineSegment> sameAngle = new ArrayList<>();
Point small = nonFinishedSegments.get(i).GetP();
Point big = nonFinishedSegments.get(i).GetQ();
int size = 2;
double angle = nonFinishedSegments.get(i).GetP().slopeTo(nonFinishedSegments.get(i).GetQ());
for (LineSegment nonFinishedSegment : nonFinishedSegments) {
if (nonFinishedSegment.GetP().slopeTo(nonFinishedSegment.GetQ()) == angle) {
sameAngle.add(nonFinishedSegment);
}
}
for (LineSegment lineSegment : sameAngle) {
if (lineSegment.GetP() == big) {
big = lineSegment.GetQ();
size++;
size++;
}