Scala class examples

Notes on Scala’s class :

  • Parameters of Scala methods are values (val) not variables (var)
  • Scala method returns the last value computed by the method

//  Scala class with a private member
class Shape{
private var vertexes = 0
}

// Scala class with a method

class Shape{
def draw() : Unit = {

// Body of the method
}
}

// Scala class with a method with a return type and constructor

class Shape{

def Shape() = {
vertexes = 0
}

def draw():Unit = {
}

def getVertexes(): Int = {
return vertexes
}

private var vertexes = 0
}

Reference :

1. Programming in Scala: A Comprehensive Step-by-step Guide

Leave a Comment

Your email address will not be published. Required fields are marked *