You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core-kotlin-modules/core-kotlin-lang-oop-3/src/test/kotlin/com/baeldung/implicitAndQualifiedthis/ImplicitAndQualifiedthisUnitTest.kt
+27-3Lines changed: 27 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ class ImplicitAndQualifiedthisUnitTest {
15
15
classOuter {
16
16
funfoo() : Int=30
17
17
val x =0
18
-
valb=this
18
+
vala=this
19
19
20
20
funprintLine() ="Member function"
21
21
@@ -28,6 +28,10 @@ class ImplicitAndQualifiedthisUnitTest {
28
28
29
29
assertEquals("Member function", this.printLine())
30
30
assertEquals("Top-level function", printLine())
31
+
32
+
funcheckThisToo(){
33
+
val c =this
34
+
}
31
35
}
32
36
33
37
innerclassInner {
@@ -48,6 +52,14 @@ class ImplicitAndQualifiedthisUnitTest {
48
52
}
49
53
50
54
funInnerLambda("test inner lambda")
55
+
56
+
val numbers =listOf(0, 1, 2, 3, 4)
57
+
numbers.forEach { number ->
58
+
number.run {
59
+
assertEquals(number, this)
60
+
}
61
+
}
62
+
51
63
}
52
64
53
65
fun Int.foo() {
@@ -56,16 +68,28 @@ class ImplicitAndQualifiedthisUnitTest {
56
68
assertEquals("Int", y::class.simpleName)
57
69
58
70
assertEquals(2, x)
59
-
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
71
+
60
72
assertEquals(42, this) // Implicit: receiver of the extension function (the Int value 42)
61
73
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
62
74
63
75
assertEquals(this, this@foo) // Both this keywords refer to the same receiver (42)
64
76
77
+
fun Int.bar() {
78
+
assertEquals(32, this) // Implicit: receiver of the extension function (the Int value 32)
79
+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
80
+
81
+
fun Int.baz(){
82
+
assertEquals(20, this) // Implicit: receiver of the extension function (the Int value 20)
83
+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
84
+
}
85
+
20.baz()
86
+
}
87
+
32.bar()
88
+
65
89
assertEquals(Inner::class.java.name, this@Inner::class.java.name) // Qualified: Inner class
66
90
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
67
91
68
-
92
+
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
69
93
assertEquals(0, this@Outer.x) // Qualified: Outer's x property
70
94
assertEquals(30, this@Outer.foo()) // Qualified: Outer's foo() function
0 commit comments