繁体   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