Java:Getters,Setters和Constructor [英] Java: Getters, Setters, and Constructor

查看:262
本文介绍了Java:Getters,Setters和Constructor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含位置的类,包括以下方法:



每个数据字段的Setters(mutators)
对于位置名称,您应该使用trim()方法来删除任何前导或尾随的空格。



每个数据字段的Getters >

构造函数:您应该只有一个构造函数,其中包含地名,纬度和经度。您可能希望数据有效,但字符串可能需要修剪。

  public class Location {
public Location(String aCity,double aLatitude,double aLongitude)
{
String city = aCity;
double latitude = aLatitude;
double longitude = aLongitude;
}
void setLocation(String theCity)
{
city = theCity.trim();
}
void setLatitude(double lat)
{
latitude = lat;
}
void setLongitude(double long1)
{
longitude = long1;
}
public String getLocation()
{
return city;
}
public double getLatitude()
{
return latitude;
}
public double getLongitude()
{
return longitude;
}
}

我不知道我会出错这里,我在两个城市,纬度和两个经度上都有错误。这是我第一次写一个类,所以请为我蠢一切。感谢您的时间。

解决方案

您几乎就在那里。您有:

  public Location(String aCity,double aLatitude,double aLongitude)
{
String city = aCity;
double latitude = aLatitude;
double longitude = aLongitude;
}

您在构造函数中声明局部变量。您实际想要声明字段:

  public class Location {

private String city;
private double latitude;
private double longitude;

public Location(String aCity,double aLatitude,double aLongitude)
{
city = aCity;
latitude = aLatitude;
longitude = aLongitude;
}

...

}


$ b b

查看有关声明成员变量的官方教程。它是简洁和写好的,将让你更好地了解发生了什么。


I'm trying to Create a class that holds a location,Including the following methods:

Setters (mutators) for each of the data fields For the location name, you should use the trim() method to remove any leading or trailing blank spaces.

Getters (accessors) for each of the data fields

Constructor: You should have only one constructor that takes the place name, the latitude, and the longitude. You may expect that the data will be valid, although the strings may need to be trimmed.

public class Location {
    public Location(String aCity, double aLatitude, double aLongitude)
    {
        String city = aCity;
        double latitude = aLatitude;
        double longitude = aLongitude;
    }
    void setLocation(String theCity)
    {
        city = theCity.trim();
    }
    void setLatitude(double lat)
    {
        latitude = lat;
    }
    void setLongitude(double long1)
    {
        longitude = long1;
    }
    public String getLocation()
    {
        return city;
    }
    public double getLatitude()
    {
        return latitude;
    }
    public double getLongitude()
    {
    return longitude;
    }
}

I'm not sure where I'm going wrong here, I'm getting errors on both citys, both latitudes, and both longitudes. This is my first time writing a class, so please dumb everything down for me. Thanks for your time.

解决方案

You are almost there. You have:

public Location(String aCity, double aLatitude, double aLongitude)
{
    String city = aCity;
    double latitude = aLatitude;
    double longitude = aLongitude;
}

You are declaring local variables in the constructor. You actually want to be declaring fields:

public class Location {

    private String city;
    private double latitude;
    private double longitude;

    public Location(String aCity, double aLatitude, double aLongitude)
    {
        city = aCity;
        latitude = aLatitude;
        longitude = aLongitude;
    }

    ...

}

Check out the official tutorial on declaring member variables. It is concise and well-written and will give you a better idea of what is going on.

这篇关于Java:Getters,Setters和Constructor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆