Consider an example. Suppose you are working with local map and use coordinates of locations and calculate distances. Of course you can remember two double values x and y for each location and write a function distance( x1, y1, x2, y2 ) that would calculate distances between two pairs of coordinates. But it is a lot more elegant to introduce a new entity that would group together the coordinates and the method of calculating distance to another location. In object-oriented programming such entity is called a class. Java class definition for the location on a local map can look like this:
class
Location {
//constructor: creates a Location object with given coordinates
Location(
double
xcoord,
double
ycoord ) {
x = xcoord;
y = ycoord;
}
//two fields of type double
double
x;
//x coordinate of the location
double
y;
//y coordinate of the location
//method (function): calculates distance from this location to another one
double
distanceTo( Location other ) {
double
dx = other.x - x;
double
dy = other.y - y;
return
sqrt( dx*dx dy*dy );
}
}
Having defined such class, we can write very simple and readable code when working with the map, like this:
Location origin = new Location( 0, 0 ); //create first location
Location destination = new Location( 250, 470 ); //create second location
double
distance = origin.distanceTo( destination );
//calculate distance
The locations origin and destination are objects and are instances of the class Location. The expression new Location( 250, 470 ) is a constructor call, it creates and returns a new instance of the class Location with the given coordinates. The expression origin.distanceTo( destination ) is a method call – it asks the object origin to calculate the distance to another object destination.
Now suppose some locations in your 2D space correspond to cities. A city has a name and population size. To efficiently manipulate cities we can extend the class
Location
by adding two more fields:
name
and
population. We will call the new agent
City. In Java this will look like:
class
City
extends
Location {
//declaration of the class City that extends the class Location
//constructor of class City
City( String n,
double
x,
double
y ) {
super( x, y );
//call of constructor of the super class with parameters x and y
name = n;
//the population field is not initialized in the constructor – to be set later
}
//fields of class City
String name;
int
population;
}
City is called a subclass of Location, and Location is correspondingly a super class (or base class) of City. City inherits all properties of Location and adds new ones. See how elegant is the code that finds the biggest city in the range of 100 kilometers from a given point of class Location (we assume that there is a collection cities where all cities are included):
int
pop = 0;
//here we will remember the largest population found so far
City biggestcity =
null;
//here we will store the best city found so far
for( City city : cities ) {
//for each city in the collection cities
if( point.distanceTo( city ) < 100 && city.population > pop ) {
//if best so far
biggestcity = city;
//remember it
pop = city.population;
//and remember its population size
}
}
traceln(
"The biggest city within 100km is "
city.name );
//print the search result
Notice that although city is an object of class City, which is "bigger" than Location, it still can be treated as Location when needed, in particular when calling the method distanceTo() of class Location.
How about vice versa? You can declare a variable of class Location and assign it an object of class City (a subclass of Location):
Location place = laJolla;
This will work. However, if you will then try to access the population of place, Java will signal an error:
int p = place.population; //error: "population cannot be resolved or is not a field"
This happens because Java does not know that place is in fact an object of class City. To handle such situations you can:
A typical code pattern is:
if( place
instanceof
City ) {
City city = (City)place;
int
p = city.population;
…
}
Virtually all objects that you use to create your models are instances of AnyLogic Java classes. Please refer to AnyLogic class reference to find the Java class names for most model elements you work with. Whenever you will need to find out what can you do with a particular object using Java, you should find the corresponding Java class in AnyLogic API reference and look through its methods and fields.