-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththisDemo.scala
68 lines (58 loc) · 1.47 KB
/
thisDemo.scala
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
package ScalaBasic
// https://www.includehelp.com/scala/this-keyword-in-scala.aspx
object thisDemo extends App {
// example 1
class ThisExample{
var id = 0
var name = ""
def this(id: Int, name: String){
this()
this.id = id
this.name = name
}
def show(){
println(id + " " +name)
}
}
val demo = new ThisExample(777, "jim")
println(demo.show())
// example 2
class Student(name: String){
def this(name: String, age: Int){
this(name)
println(name + " " + age)
}
}
val demo2 = new Student("kim", 999)
println(demo2)
// example 3 : With (.) dot operator
class teachers{
var name = ""
var marks = 0
def info(name: String, marks: Int){
this.name = name
this.marks = marks
}
def show(){
println("teachers " + name + " has obtained " + marks + " marks")
}
}
val demo3 = new teachers()
demo3.info("amy", 333)
println(demo3.show())
//example 4 : Using this()
class teachers2{
var name = ""
var marks = 0
def this(name: String, marks: Int){
this()
this.name = name
this.marks = marks
}
def show(){
println("teachers " + name + " has obtained " + marks + " marks")
}
}
val demo4 = new teachers2("kate", 555)
println(demo4.show())
}