-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFirstSteps03Classes.sc
91 lines (82 loc) · 4.67 KB
/
FirstSteps03Classes.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package day01session01.first_steps.solution
/*
* This file is part of the "Scala Hack Session".
*
* Scala Hack Session is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scala Hack Session is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scala Hack Session. If not, see <http://www.gnu.org/licenses/>.
*
* =================================================================================
*
*/
object FirstSteps03Classes {
//class definitions
//inside the class define:
// - dateFormat and
// - constructionTime
//like in the previous exercise
class ScalaFun {
//unlike Java imports can also be defined within a class or function
import java.text.SimpleDateFormat
private val alive = true
//define variables for dateFormat and constructionTime here
var constructionTime = System.currentTimeMillis()
val dateFormat = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss:SSS")
//Constructors with params are defined using "def this(params)".
//The first call must always be the primary constructor "this()"
def this(constructionTime: Long) = {
this()
this.constructionTime = constructionTime + 1000
}
}
//now instantiate the class and have a look at the output
//note: in Scala for default constructors a "new MyClass" without brackets is just fine
//in Scala
val instance = new ScalaFun //> instance : day01session01.first_steps.solution.FirstSteps03Classes.ScalaFu
//| n = day01session01.first_steps.solution.FirstSteps03Classes$ScalaFun@b27868
//| b
//do a println using the instance' dateFormat and constructionTime
println(instance.dateFormat.format(instance.constructionTime))
//> 19:02:2014 17:41:11:796
//Example taken from: http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html
//Case classes: adding the "case" modifier to a class lets the compiler add some syntactic conveniences to the class:
//- a factory method making instantiation easier: "CaseClass(param)" instead of "new CaseClass(param)"
//- all arguments get a "val" prefix and can be publicly accessed as fields
//- adds "natural" implementations of toString, hashCode and equals.
//case classes are always structually (deep) compared (using "equals" resp. "==")
abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class VarNumber(num: Double, name: String) extends Expr
//instantiate Number and Var and print them out
val num = Number(2.5) //> num : day01session01.first_steps.solution.FirstSteps03Classes.Number = Num
//| ber(2.5)
val variable = VarNumber(2.5, "varNum") //> variable : day01session01.first_steps.solution.FirstSteps03Classes.VarNumb
//| er = VarNumber(2.5,varNum)
//print
println(s"$num and $variable") //> Number(2.5) and VarNumber(2.5,varNum)
//another convenience is the copy function of case classes where you can call caseClass.copy(param1Value, param2Value, ...)
//the copy function requires at least one param, are others will then be copied
//in case a specific param should be overridden then use: copy(param3=value, param2=value)
val varNum1 = VarNumber(2.4, "varNum1") //> varNum1 : day01session01.first_steps.solution.FirstSteps03Classes.VarNumbe
//| r = VarNumber(2.4,varNum1)
//copy with first value changed
val varNum2 = varNum1.copy(2.5) //> varNum2 : day01session01.first_steps.solution.FirstSteps03Classes.VarNumbe
//| r = VarNumber(2.5,varNum1)
//call copy with only the second value changed
val varNum3 = varNum1.copy(name="varNum2") //> varNum3 : day01session01.first_steps.solution.FirstSteps03Classes.VarNumbe
//| r = VarNumber(2.4,varNum2)
case class Super
//try to define a case class extending from super
//this will break. have a look at the error message
//case class Child extends Super
}