Java Reference
In-Depth Information
String name
String city
String state
String team
double latitude
double longitude
String toString() { "($team,$name,$latitude,$longitude)" }
}
If you're used to Java, what's conspicuous here is what's absent. The lack of semicolons
is probably not surprising at this point. What may be a surprise is that there are no public
or private access modifiers anywhere. In Groovy, if you don't specify an access modifier,
attributes are assumed to be private, and methods are assumed to be public. [ 4 ]
4 That's another “duh” moment. The default access in Java is “package private,” which means the member is ac-
cessible from any other class in the same subdirectory. In roughly 15 years of Java coding I've used this access
deliberately maybe twice, and both times there were reasonable alternatives. I can understand trying to create some
sort of friend access, but why make it the default? Once again, Groovy does what makes sense.
You might also note that there are no constructors in the Stadium class. In Java, if you
don't add a constructor, the compiler gives you a default constructor for free. In Groovy,
however, you get not only the default, but also a map-based constructor that allows you to
set any combination of attribute values by supplying them as key-value pairs.
With this in mind, here's the first part of the script to populate a database table with the
Stadium locations:
def stadiums = []
stadiums <<
new Stadium(name:'Angel Stadium',city:'Anaheim',state:'CA',team:'ana')
stadiums <<
new Stadium(name:'Chase Field',city:'Phoenix',state:'AZ',team:'ari')
...
stadiums <<
new Stadium(name:'Rogers Centre',city:'Toronto',state:'ON',team:'tor')
stadiums <<
new Stadium(name:'Nationals Park',
city:'Washington',state:'DC',team:'was')
The stadiums variable is initialized to an empty java.util.ArrayList . The left-
shift operator has been implemented in Collection to be an append method, so the rest
of the listing instantiates each of the MLB stadiums and appends it to the list.
 
Search WWH ::




Custom Search