所以我试图做到这一点,当用户创建一个新的PlaceInformation对象时,输入的经度和纬度被存储在GeoLocation对象的地方。显然,在运行“客户端”代码时,首先初始化GeoLocation对象,并赋予值0.0,0.0而不是用户输入的值。我该如何使GeoLocation对象中的构造函数存储的纬度和经度参数?我尝试了不同的方式,但有一些范围问题。对象内的对象java
public class PlaceInformation {
private String name;
private String tag;
private String address;
private double latitude;
private double longitude;
GeoLocation place = new GeoLocation(latitude, longitude);
public PlaceInformation(String name, String address, String tag,
double latitude, double longitude) {
this.name = name;
this.address = address;
this.tag = tag;
this.latitude = latitude;
this.longitude = longitude;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getTag() {
return tag;
}
public String toString() {
return name + ", " + address;
}
public double test() {
return place.getLongitude();
}
public double distanceFrom(GeoLocation spot) {
return spot.distanceFrom(place);
}
}
好的,我明白了,我没有意识到你可以在不提供参数的情况下实例化对象。所以实质上,直到用户创建PlaceInformation对象,GeoLocation对象只是空白并等待被填充? – Nickomang
没问题。直到您实例化GeoLocation对象并将其分配给位置之前,将点指向空引用。因为'place'是一个实例变量,所以您不必担心'NullPointerException',因为类必须实例化才能使用。您可能考虑的另一件事是从PlaceInformation类中移除'latitute'和'longitude'字段,因为这些值存储在'place'中。 –
是的,我可以看到现在这些是多余的。感谢您的帮助,我非常感谢快速回复! – Nickomang