Classes in Scala

Classes are considered as a blueprint and then you instantiate this class in order to create something that will actually be represented in memory. They can contain methods, values, variables, types, objects, traits, and classes which are collectively called members. Let's demonstrate this with the following example:

class Animal {
var animalName = null
var animalAge = -1
def setAnimalName (animalName:String) {
this.animalName = animalName
}
def setAnaimalAge (animalAge:Int) {
this.animalAge = animalAge
}
def getAnimalName () : String = {
animalName
}
def getAnimalAge () : Int = {
animalAge
}
}

We have two variables animalName and animalAge with their setters and getters. Now, how do we use them to solve our purpose? Here come the usages of Scala objects. Now, we will discuss Scala objects, then we will trace back to our next discussion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset