如何在主语句中将对象连接在一起?

时间:2019-01-11 22:01:38

标签: java

我教AP CSA(一年级),并决定给孩子们一个挑战性的任务(至少对我来说)。他们应该使用产生打印语句,该语句将告诉小人行进特定距离需要多长时间。在给他们之前,我想有一个解决方案。

打印语句对我来说很容易,并且程序按“预期的方式”工作-我禁不住觉得我太复杂了,并且失去了一点抽象的机会,而不是对我的经度和纬度进行硬编码。我是否有更好的方法将GeoLocation起点和GeoLocation终点对象连接到我的城市?请参阅下面的代码。

    GeoLocation start = new GeoLocation(37.765469, 100.015167);
    GeoLocation end = new GeoLocation(37.275280, 107.880066);
    double distance = start.distanceFrom(end);
    double travelTime = distance/15;
    int travelReport = (int)travelTime;


    WesternTown sweatyPost = new WesternTown();
    sweatyPost.saloons = 2;
    sweatyPost.sheriffs = 1;
    sweatyPost.troublemakers = 5;

    WesternTown dodgeCity = new WesternTown();
    dodgeCity.saloons = 7;
    dodgeCity.sheriffs = 2;
    dodgeCity.troublemakers = 29;
    dodgeCity.longitude = 100.015167;
    dodgeCity.latitude = 37.765469;

    WesternTown durango = new WesternTown();
    durango.saloons = 4;
    durango.sheriffs = 0;
    durango.troublemakers = 6;
    durango.longitude = 107.880066;
    durango.latitude = 37.275280;

2 个答案:

答案 0 :(得分:3)

您可以看到GeoLocationWesternTown的属性,因此可以说是一个属性:

public class WesternTown{
    private int saloons;
    private int sheriffs;
    private int troublemakers;
    private GeoLocation location;

    // appropriate constructor with all :
    public WesternTown(int saloons, int sheriffs, int troublemakers, Geolocation location){
        this.saloons = saloons;
        ...
    }
}

您将拥有

WesternTown dodgeCity = new WesternTown(7, 2, 29, new GeoLocation(37.765469, 100.015167));
WesternTown durango = new WesternTown(4, 0, 6, new GeoLocation(37.275280, 107.880066));

// 1. Leave method in GeoLocation class
double distance = dodgeCity.getLocation().distanceFrom(durango.getLocation());
// 2. or move it into WesternTown
double distance = dodgeCity.distanceFrom(durango);

  1. 将方法保留在GeoLocation类中

    double distanceFrom(Geolocation other){
        return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
    }
    
  2. 或将其移至WesternTown

    // move all the method
    double distanceFrom(WesternTown other){
        return Math.sqrt(Math.pow(this.location.getX() - other.location.getX(), 2) + 
                         Math.pow(this.location.getY() - other.location.getY(), 2));
    }
    
    // or just call the Geolocation method
    double distanceFrom(WesternTown other){
        return this.location.distanceFrom(other.location);
    }
    

答案 1 :(得分:1)

GeoLocation添加一个私有WesternTown属性,并创建一个公共方法setLocation(GeoLocation location)来设置位置。这样一来,您就可以在GeoLocation内完成任何验证,并且无需自己设置经纬度。

然后您可以简单地说出dodgeCity.setLocation(start)durango.setLocation(end)

或更妙的是,将GeoLocation包含在构造函数中,然后完全不需要设置器。这是可取的,因为城镇一旦创建便无法更改其位置,这实际上是没有意义的。

class WesternTown {
    //other properties...
    private GeoLocation location; //<- make sure this is PRIVATE not PUBLIC

    public WesternTown(GeoLocation location) {
        this.location = location;
    }

    public GeoLocation getLocation() {
        return location;
    }
}

如果您选择这条路线,请删除默认的构造函数(如果有),以确保创建的所有城镇都具有位置。

现在,您可以轻松计算任意两个城市之间的距离:

double distance = dodgeCity.getLocation().distanceFrom(durango.getLocation());

虽然也不是很关键,但您可能应该将其他字段设为私有,并为它们也包括设置器/获取器。它是更多的代码,但是它促进了更好的实践,并使它们习惯于调用方法与对象的状态进行交互的想法。