Last active
October 31, 2023 16:20
-
-
Save rodobarcaaa/736ea1e93bb9239344509b8249a4cd39 to your computer and use it in GitHub Desktop.
Scala Classes Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Classes { | |
class MyFirstClass | |
val myFirstClass = new MyFirstClass | |
//---------------------------------------------// | |
class Vehicle( | |
var passengers: Int, //number of passengers | |
var speed: Int, //speed value | |
val unit: String //speed unit | |
) { | |
override def toString: String = s"(passengers: $passengers, speed: $speed $unit)" | |
} | |
val bicycle = new Vehicle(1, 30, "km") | |
bicycle.passengers // 1 | |
bicycle.passengers = 2 | |
bicycle.passengers // 2 | |
println(bicycle) // (passengers: 2, speed: 30 km) | |
//---------------------------------------------// | |
class Vehicle1( | |
var passengers: Int, | |
private val speed: Int, | |
private val unit: String = "km" | |
) { | |
val speedDescription: String = s"$speed $unit" | |
override def toString: String = s"(passengers: $passengers, speed: $speedDescription)" | |
} | |
object Vehicle1 { | |
def speed220km(passengers: Int): Vehicle1 = new Vehicle1(passengers, 220, "km") | |
} | |
val motorcycle = new Vehicle1(2, 100) | |
motorcycle.passengers // 2 | |
motorcycle.speed // don't compile because the attribute is private | |
motorcycle.speedDescription // 100 km | |
println(motorcycle) // (passengers: 2, speed: 100 km) | |
val car: Vehicle1 = Vehicle1.speed220km(5) | |
println(car) // (passengers: 5, speed: 220 km) | |
//---------------------------------------------// | |
case class Vehicle2(passengers: Int, speed: Int, unit: String) { | |
val speedDescription: String = s"$speed $unit" | |
} | |
// Normal constructor and the most used | |
val myCar = Vehicle2(5, 200, "km") | |
// Using apply explicitly | |
val myCar1 = Vehicle2.apply(5, 200, "km") | |
// By "tuple" of values | |
val myCar2 = Vehicle2.tupled((5, 200, "km")) | |
// Through currying mode parameters | |
val myCar3 = Vehicle2.curried(5)(200)("km") | |
// use the automatically generated methods | |
myCar.passengers // 5 | |
myCar.speed // 200 | |
myCar.speed = 300 // don't compile -> error: reassignment to val | |
myCar.unit // km | |
println(myCar) // Vehicle2(5,200,km) | |
val myFastCar = myCar.copy(passengers = 2, speed = 320) | |
println(myFastCar) // Vehicle2(2,320,km) | |
// compare by structure and not by reference | |
myCar == myCar1 // true | |
myCar == myCar2 // true | |
myCar == myCar3 // true | |
myCar == myFastCar // false | |
// use the unapply in match expressions | |
def recognizeVehicle(x: Vehicle2): String = x match { | |
case Vehicle2(10, speed, unit) => | |
s"10 passenger minivan with speed of $speed $unit" | |
case Vehicle2(2, speed, _) if speed > 300 => | |
s"High speed sports car ${x.speedDescription}" | |
case _ => | |
"Any non-minivan or sports car: " + x | |
} | |
val minivan = Vehicle2(10, 100, "km") | |
println(recognizeVehicle(minivan)) | |
// 10 passenger minivan with speed of 100 km | |
println(recognizeVehicle(myFastCar)) | |
// High speed sports car 320 km | |
println(recognizeVehicle(myCar)) | |
// Any non-minivan or sports car: Vehicle2(5,200,km) | |
//--------------------------------------------------// | |
sealed trait Animal { | |
def name: String | |
} | |
case class Dog(name: String, owner: String) extends Animal | |
case class Cat(name: String, color: String) extends Animal | |
def recognizeAnimal(a: Animal): String = a match { | |
case Dog(name, owner) => s"The dog $name belongs to $owner." | |
case Cat(name, color) => s"${name.capitalize} is a very beautiful $color cat." | |
} | |
val bony = Dog("Bony", "Pedrito") | |
val tom = Cat("tom", "negro") | |
println(recognizeAnimal(bony)) | |
// The dog Bony belongs to Pedrito. | |
println(recognizeAnimal(tom)) | |
// Tom is a very beautiful black cat. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment