2013-11-27 114 views
1

所以我试图做到这一点,当用户创建一个新的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); 
    } 
} 

回答

4

在构造函数中实例化对象。

public class PlaceInformation { 
     private String name; 
     private String tag; 
     private String address; 
     private double latitude; 
     private double longitude; 
     GeoLocation place; 


     public PlaceInformation(String name, String address, String tag, 
          double latitude, double longitude) { 
      place = new GeoLocation(latitude, longitude); 
      this.name = name; 
      this.address = address; 
      this.tag = tag; 
      this.latitude = latitude; 
      this.longitude = longitude; 
     } 

     /* Rest of class omitted */ 
} 

另外请注意,你可能想使place私人。

+0

好的,我明白了,我没有意识到你可以在不提供参数的情况下实例化对象。所以实质上,直到用户创建PlaceInformation对象,GeoLocation对象只是空白并等待被填充? – Nickomang

+0

没问题。直到您实例化GeoLocation对象并将其分配给位置之前,将点指向空引用。因为'place'是一个实例变量,所以您不必担心'NullPointerException',因为类必须实例化才能使用。您可能考虑的另一件事是从PlaceInformation类中移除'latitute'和'longitude'字段,因为这些值存储在'place'中。 –

+0

是的,我可以看到现在这些是多余的。感谢您的帮助,我非常感谢快速回复! – Nickomang

0

如何在PlaceInformation构造函数初始化的地方场:

public class PlaceInformation { 
    private String name; 
    private String tag; 
    private String address; 
    private double latitude; 
    private double longitude; 
    GeoLocation place = null; 


    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; 
     place = new GeoLocation(latitude, longitude); 
    } 
0

你应该申报地理定位的私人领域。然后,在构造函数中创建对象的新实例。然后,其他方法将能够“看到”地方变量。结论是,这不是“对象内的对象”,而是从一个对象到另一个对象的引用。

public class PlaceInformation { 
    private String name; 
    private String tag; 
    private String address; 
    private double latitude; 
    private double longitude; 
    private GeoLocation place; 


    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; 
     this.place = new GeoLocation(latitude, 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); 
    } 
}