APPENDIX C: Introduction to Scala 433
- val vehicle2 = new Bike(100)
- val vehicle3 = new Batmobile(300)
- val vehicleList = List(vehicle1, vehicle2, vehicle3)
- val fastestVehicle = vehicleList.maxBy(_.mph)
- printVehicle
- def printVehicle{
- println ("speed of Bike : " + vehicle1.mph);
- println ("speed of Car : " + vehicle2.mph);
- println ("speed of Batmobile : " + vehicle3.mph);
- println ("Fastest Vehicle : " + fastestVehicle.mph + " mph");
- }
- }
- }
When the previous code is compiled and executed, it produces the following result:
scalac vehicle.scala
scala Vehicle
speed of Bike : 200 mph
speed of Car : 100 mph
speed of Batmobile : 300 mph
Fastest Vehicle : 300 mph
Summary
This appendix introduced you to the basics of Scala. You learned three ways to interactively
execute Scala code as a script and as a compiled program. Then you learned how to use the Scala
collections library. Finally, you learned how to use traits and how to use a singleton object in an
application.