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-10/src/test/kotlin/com/baeldung/variableshadowing/VariableShadowingUnitTest.kt
+10-8Lines changed: 10 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -12,18 +12,19 @@ class VariableShadowingUnitTest{
12
12
val number =10// Top-level variable
13
13
14
14
fungetNumber() : Int { // top level function
15
-
val number =20
15
+
val number =20// shadowing top level variable
16
16
return number
17
17
}
18
18
19
19
assertEquals(20, getNumber())
20
20
assertEquals(10, number)
21
21
22
+
// shadowing class member
22
23
classCar {
23
24
val speed:Int=100
24
25
25
26
funupSpeed() : Int {
26
-
val speed = speed *2// Shadowing the constructor parameter 'speed'
27
+
val speed = speed *2// Shadowing class member -> speed
27
28
return speed
28
29
}
29
30
}
@@ -49,8 +50,10 @@ class VariableShadowingUnitTest{
49
50
50
51
calculateTotalPrice(20)
51
52
52
-
// in loop
53
+
53
54
val numbers =listOf(1, 2, 3, 4, 5)
55
+
56
+
// shadowing in loop
54
57
for (number in numbers) {
55
58
val doubledNumber = number *2
56
59
val number = number *2// Shadowing the loop variable 'number'
@@ -59,7 +62,7 @@ class VariableShadowingUnitTest{
59
62
}
60
63
61
64
62
-
// extension
65
+
//shadowing in extension
63
66
assertEquals(15, numbers.sum())
64
67
65
68
fun List<Int>.sum(): Int { // Shadowing occur in here
@@ -70,12 +73,11 @@ class VariableShadowingUnitTest{
0 commit comments