簡體   English   中英

對象java中的對象

[英]object within object java

因此,我試圖使其成為當用戶創建新的PlaceInformation對象時,輸入的緯度和經度存儲在GeoLocation對象位置內。 顯然,在運行“客戶端”代碼時,首先初始化GeoLocation對象,並給出值0.0,0.0而不是用戶輸入的值。 如何使構造函數中的緯度和經度參數存儲在GeoLocation對象中? 我嘗試了另一種方式,但有一些范圍問題。

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);
    }
}

在構造函數中實例化對象。

    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設為私有。

如何在PlaceInformation構造函數中初始化place字段:

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);
    }

您應該將Geolocation聲明為私有字段。 然后,在構造函數中,您將創建該對象的新實例。 然后,其他方法將能夠“看到”地方變量。 總而言之,這不是“對象中的對象”,而是從一個對象到另一個對象的引用。

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM