我正在用Java做一个基本的GPS流程项目。它的点类是当前位置,如下所示;
import static java.lang.Math.*;
public class Point {
private static final double MIN_LONGITUDE = -180.0;
private static final double MAX_LONGITUDE = 180.0;
private static final double MIN_LATITUDE = -90.0;
private static final double MAX_LATITUDE = 90.0;
private static final double MEAN_EARTH_RADIUS = 6.371009e+6;
// TODO: Define fields for time, longitude, latitude and elevation
public static ZonedDateTime time = ZonedDateTime.now();
public static double longitude;
public static double latitude;
public static double elevation;
// TODO: Define a constructor
public Point(ZonedDateTime timestamp, double longitude, double latitude, double elevation) {
if( longitude < MIN_LONGITUDE || longitude > MAX_LONGITUDE || latitude < MIN_LATITUDE || latitude > MAX_LATITUDE)
throw new GPSException("Longitude or latitude is incorrect");
this.longitude = longitude;
this.latitude = latitude;
this.elevation = elevation;
}
public Point() {
this(time, -1.54853, 53.80462, 72.5);
}
// TODO: Define getters for the fields
public static ZonedDateTime getTime() {
return time;
}
public static double getLongitude() {
return longitude;
}
public static double getLatitude() {
return latitude;
}
public static double getElevation() {
return elevation;
}
// TODO: Define a toString() method that meets requirements
public String toString() {
return "( " + longitude + ", " + latitude + " ), " + elevation + " )";
}
然后有一个叫做Track的类,它是显示旅程的Points的集合。
class Track {
public LinkedList<Point> Track = new LinkedList<Point>();
public void add(Object Point) {
Track.add((Point) Point);
}
public Object get(int I) {
return Track.get(I);
}
public Integer size() {
int Size = Track.size();
return Size;
}
public void readFile(String filename)
throws IOException {
int i = 0;
ArrayList<String> textFile = new ArrayList<>();
Scanner input = new Scanner(System.in);
File file = new File(input.nextLine());
input = new Scanner(filename);
while (input.hasNext()) {
String letter = input.next();
textFile.add(i, letter);
i++;
}
input.close();
for (int j = 1; j < textFile.size(); j++) {
ZonedDateTime times;
double longitude = 0;
double latitude;
double elevation;
String s = textFile.get(j);
String[] half = s.split(",", 4);
times = ZonedDateTime.parse(half[0]);
longitude = Double.parseDouble((half[1]));
latitude = Double.parseDouble((half[2]));
elevation = Double.parseDouble((half[3]));
Point point = new Point(times, longitude, latitude, elevation);
Track.add(point);
}
}
public String toString() {
return "( " + Point.longitude + ", " + Point.latitude + " ), " + Point.elevation + " )";
}
}
class Main {
public static void main(String[] args) throws IOException {
ZonedDateTime time = Point.getTime();
Track track1 = new Track();
Point point1 = new Point(time, -1.54853, 53.80462, 72.5);
track1.add(point1);
System.out.println("\n" + track1);
System.out.println(point1.getElevation());
track1.get(0).getElevation();
}
}
main的最后一行是给我一个错误无法解决的方法。它似乎是由getElevation引起的,但是我不确定如何解决它。任何帮助,将不胜感激。我已经在实际点上测试了get标高方法,并且效果很好。
答案 0 :(得分:0)
因为您的呼叫track1.get(0)
返回了Object
而没有getElevation
方法。您可能想在此处返回Point
class Track {
public Object get(int I) {
return Track.get(I);
}
}
此外,您的方法主体没有任何意义。
答案 1 :(得分:0)
您必须强制转换get方法的结果:例如:
class Main {
public static void main(String[] args) throws IOException {
ZonedDateTime time = Point.getTime();
Track track1 = new Track();
Point point1 = new Point(time, -1.54853, 53.80462, 72.5);
track1.add(point1);
System.out.println("\n" + track1);
System.out.println(point1.getElevation());
if(track1.get(0) instanceof Point) { //Making sure we can cast the object
(Point)(track1.get(0)).getElevation();
}
}