Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 18: Stateful Objects > 18.3 Case study: Discrete event simulation - Pg. 367

S ECTION 18.3 · Case study: Discrete event simulation The celsius variable definition is followed by a getter, " fahrenheit ", and a setter, " fahrenheit_= ", which access the same temperature, but in de- grees Fahrenheit. There is no separate field that contains the current temper- ature value in Fahrenheit. Instead the getter and setter methods for Fahren- heit values automatically convert from and to degrees Celsius, respectively. Here's an example of interacting with a Thermometer object: scala> val t = new Thermometer t: Thermometer = 32.0F/0.0C scala> t.celsius = 100 scala> t res3: Thermometer = 212.0F/100.0C scala> t.fahrenheit = -40 scala> t res4: Thermometer = -40.0F/-40.0C