简体   繁体   English

对象java中的对象

[英]object within object java

So I'm trying to make it so that when the user creates a new PlaceInformation object, the inputted latitude and longitude are stored inside of the GeoLocation object place. 因此,我试图使其成为当用户创建新的PlaceInformation对象时,输入的纬度和经度存储在GeoLocation对象位置内。 Obviously, upon running the "client" code, the GeoLocation object is initialized first and is given a value of 0.0, 0.0 instead of what the user inputs. 显然,在运行“客户端”代码时,首先初始化GeoLocation对象,并给出值0.0,0.0而不是用户输入的值。 How do I make it so that the latitude and longitude parameters from the constructor store in the GeoLocation object? 如何使构造函数中的纬度和经度参数存储在GeoLocation对象中? I tried it a different way but there were some scope issues. 我尝试了另一种方式,但有一些范围问题。

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

Instantiate the object within the constructor. 在构造函数中实例化对象。

    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 */
}

Also note that you may want to make the place private. 另请注意,您可能希望将该place设为私有。

How about initializing place field in PlaceInformation constructor: 如何在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);
    }

You should declare Geolocation a private field. 您应该将Geolocation声明为私有字段。 Then, in the constructor you create a new instance of the object. 然后,在构造函数中,您将创建该对象的新实例。 Then, the other methods will be able to "see" the place variable. 然后,其他方法将能够“看到”地方变量。 Concluding, this is not "An object within an object" but a reference from one object to another. 总而言之,这不是“对象中的对象”,而是从一个对象到另一个对象的引用。

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