Download apply object-oriented programming
Transcript
23 }
24
25 interface Vehicle {
26
public void turn(float radians);
27
public void accelerate(float rate);
28
public float getSpeed();
29 }
Listing 3.5: ”Interfaces 1”
In listing 3.5, both the Skateboard and the Bicycle classes implement the Vehicle interface. If you had an
agent based simulation of how youth move around in a community, you could model a person as:
1 class Teenager {
2
Vehicle modeOfTransportation;
3
4
// rest of the details that define teenagers go here.
5 }
Listing 3.6: ”Teenager”
e agent based model could construct a number of instances of the teenager, randomly giving each
teenager object either a Skateboard or a Bicycle as that teenager’s modeOf Transportation. e teenager
(in this very simplified model) doesn’t care if his modeOf Transportation is a skateboard or a bicycle, all
he cares is that he can use it to get around town and interact with other agents.
e example listings 3.5 and 3.6 above were written in the Java programming language. In Java, each class
has to explicitly enumerate the interfaces that it supports. While this works, it is not the only way to
implement interfaces.
3.3.3 Go
e programming language Go includes a unique implementation of interfaces. In most languages that
support interfaces, like Java and C#, each class must explicitly enumerate the interfaces it supports. In Go,
27