[英]How can I connect objects together within a main statement?
我教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;
您可以将GeoLocation
视为WesternTown
的属性,因此可以将其视为属性:
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);
GeoLocation类中的离开方法
double distanceFrom(Geolocation other){ return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 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); }
向WesternTown
添加私有的GeoLocation
属性,并创建一个公共方法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());
同样,尽管并不十分关键,但您可能应该将其他字段设为私有,并为它们添加设置器/获取器。 它是更多的代码,但是它促进了更好的实践,并使它们习惯于调用方法与对象的状态进行交互的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.