-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyClass.kt
189 lines (149 loc) · 3.71 KB
/
MyClass.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class MyClass {
fun test() {
println("Class with multiple constructors")
val frank = Person("Frank", "S", "Watson")
println(frank.fullName())
println("")
println("Inheritance")
val wood = Student("Kent", "Wood")
println(wood.fullName())
var bar1 = Bar1(0)
var bar2 = Bar2()
println("")
println("Inheritance (Child -> Parent -> Grandparent)")
println("0. Initialise Child")
val child = Child("memo1")
println("1. Override function in Child")
child.f()
println("2. Inherited function from Parent")
child.g()
println("3. Call both super and override function in Child")
child.h()
println("4. Call both super and override function in inner Class of Parent")
val parent = Parent("memo2")
val colleague = parent.Colleague()
colleague.g()
println("")
println("Resolving overriding conflicts")
val c = C()
c.foo()
c.bar()
println("----")
val d = D()
d.foo()
d.bar()
}
}
open class Person {
var firstName: String = ""
var middleName: String = ""
var lastName: String = ""
constructor(firstName: String, lastName: String) {
this.firstName = firstName
this.lastName = lastName
}
constructor(firstName: String, middleName: String, lastName: String) : this(firstName, lastName) {
this.middleName = middleName
}
var fullName: () -> String = {
"$firstName $middleName $lastName"
}
}
class Student : Person {
constructor(firstName: String, lastName: String) : super(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
constructor(firstName: String, middleName: String, lastName: String) : this(firstName, lastName) {
this.middleName = middleName
}
}
interface Foo {
val count: Int
}
class Bar1(override val count: Int) : Foo
class Bar2 : Foo {
override val count = 0
}
open class Grandparent(memo: String) {
init {
println("Initialising Grandparent")
}
open val id: Int = (0..10000).random().also { println("Initialising id in Grandparent: $it") }
open fun f() {
println("Grandparent.f()")
}
}
open class Parent(memo: String) : Grandparent(memo) {
init {
println("Initialising Parent")
}
override val id: Int = (0..10000).random().also { println("Initialising id in Parent: $it") }
override fun f() {
println("Parent.f()")
}
fun g() {
print("From Grandparent: ")
super.f()
print("From Child: ")
f()
}
inner class Colleague {
fun g() {
print("From Grandparent: ")
print("From Parent: ")
f()
}
}
}
class Child(memo: String) : Parent(memo) {
init {
println("Initialising Child")
}
override val id: Int = (0..10000).random().also { println("Initialising id in Child: $it") }
override fun f() {
println("Child.f()")
}
fun h() {
print("From Parent: ")
super.f()
print("From Child: ")
f()
}
}
abstract class Base {
abstract fun f()
}
class Derived : Base() {
override fun f() {
}
}
interface A {
fun foo() {
println("A")
}
fun bar()
}
interface B {
fun foo() {
println("B")
}
fun bar() {
println("bar")
}
}
class C : A {
override fun bar() {
println("bar")
}
}
class D : A, B {
override fun foo() {
super<A>.foo()
super<B>.foo()
}
override fun bar() {
super.bar() // from B
}
}