Let's continue our exploration of Kotlin objects, which combine state and behavior. We've seen how to create an object that stores a single value. Now let's see how to create an object that implements a single method acting on stored data. Again, this is not representative of the true power of Kotlin objects, but we're just getting started!
Create a class called Simple
.
Simple
should store a single Int
value which you can initialize to 0.
It should also provide a public method called squared
that takes no parameters and returns the stored value
,
squared.
Stuck? You may find these lessons helpful: