This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
21_visibility.kt
96 lines (82 loc) · 1.72 KB
/
21_visibility.kt
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
92
93
94
95
96
/* Output:
Nested init
234
Nested init
com.moshbit.kotlift.Outer$Nested@4c5e43ee (or similar)
Nested init
5
78910348910
*/
// Uncommented code will not compile due to visibility modifiers
open internal class Outer {
private val a = 1
protected val b = 2
internal val c = 3
val d = 4 // public by default
protected val n = Nested()
protected class Nested {
internal val e: Int = 5
init {
println("Nested init")
}
}
private fun o(): Int {
return 6
}
protected fun p(): Int {
return 7
}
internal fun q(): Int {
return 8
}
fun r(): Int {
return 9
}
public fun s(): Int {
return 10
}
}
protected class Subclass : Outer() {
// a is not visible
// b, c and d are visible
// Nested and e are visible
fun printAll() {
// println(a)
print(b)
print(c)
println(d)
println(Outer.Nested())
println(Nested().e)
// print(o())
print(p())
print(q())
print(r())
print(s())
}
}
class Unrelated(val o: Outer) {
// o.a, o.b are not visible
// o.c and o.d are visible (same module)
// Outer.Nested and Nested::e are not visible. In Swift they are visible, as there is no Protected.
fun printAll() {
// println(o.a)
// println(o.b) // This statement runs in Swift, as there is no Protected.
print(o.c)
print(o.d)
/* // It is OK that the following 3 lines run in Swift:
val nested = Outer.Nested()
println(nested)
println(nested.e)*/
// print(o.o())
// print(o.p()) // This statement runs in Swift, as there is no Protected.
print(o.q())
print(o.r())
print(o.s())
}
}
fun main(args: Array<String>) {
val x = Subclass()
x.printAll()
val y = Unrelated(o: x)
y.printAll()
}