-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.html
More file actions
388 lines (316 loc) · 13.9 KB
/
class.html
File metadata and controls
388 lines (316 loc) · 13.9 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html lang="en">
<head>
<title>Class Kotlin</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Merriweather+Sans:400,300,700" type="text/css">
<link rel="stylesheet" href="static/app.css" type="text/css">
<link rel="shortcut icon" href="static/logo.png" />
<meta name="theme-color" content="#111111">
<meta name="og:type" content="website">
<meta name="og:title" content="MoraSoftware.github.io">
<meta name="og:description" content="Mora Software">
<meta name="og:site_name" content="Mora Software">
<meta name="og:url" content="{{ site.url }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="block">
<form action="index.html">
<input type="image" src="static/white.jpg" />
</form>
</div>
<div class="block">
<h2>Functions Kotlin</h2>
</div>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#init">Init</a></li>
<li><a data-toggle="tab" href="#inheritance">Inheritance</a></li>
<li><a data-toggle="tab" href="#override">Override</a></li>
<li><a data-toggle="tab" href="#InheritanceSecondary">Inheritance Secondary Constructors</a></li>
<li><a data-toggle="tab" href="#visibility">Visibility</a></li>
<li><a data-toggle="tab" href="#abstract">Abstract</a></li>
<li><a data-toggle="tab" href="#interface">Interface</a></li>
<li><a data-toggle="tab" href="#data">Data Clases</a></li>
<li><a data-toggle="tab" href="#sealed">Sealed</a></li>
</ul>
<div class="tab-content">
<div id="init" class="tab-pane fade in active">
<h3>Class, Primary Constructor, Secondary Constructor and Init Block</h3>
<xmp>
fun main(args: Array<String>) {
var student = Student("Steve", 10)
println(student.id)
}
class Student(var name: String) {
var id: Int = -1
init {
println("Student has got a name as $name and id is $id")
}
constructor(n: String, id: Int): this(n) {
// The body of the secondary constructor is called after init block
this.id = id
}
}
</xmp>
</div>
<div id="inheritance" class="tab-pane fade">
<h3>Inheritance</h3>
<xmp>
fun main(args: Array<String>) {
var dog = Dog()
dog.bread = "labra"
dog.color = "black"
dog.bark()
dog.eat()
var cat = Cat()
cat.age = 7
cat.color = "brown"
cat.meow()
cat.eat()
var animal = Animal()
animal.color = "white"
animal.eat()
}
open class Animal { // Super class / Parent class / Base class
var color: String = ""
fun eat() {
println("Eat")
}
}
class Dog : Animal() { // Sub class / Child class / Derived class
var bread: String = ""
fun bark() {
println("Bark")
}
}
class Cat : Animal() { // Sub class / Child class / Derived class
var age: Int = -1
fun meow() {
println("Meow")
}
}
</xmp>
<hr>
<p>by default Class are:</p>
<p>*public</p>
<p>*final</p>
<p>So for inheritance you need to make a class --OPEN--</p>
</div>
<div id="override" class="tab-pane fade">
<h3>Override</h3>
<xmp>
fun main(args: Array<String>) {
var dog = MyDog()
println(dog.color)
dog.eat()
}
open class MyAnimal {
open var color: String = "White"
open fun eat() {
println("Animal Eating")
}
}
class MyDog : MyAnimal() {
var bread: String = ""
override var color: String = "Black"
fun bark() {
println("Bark")
}
override fun eat() {
super<MyAnimal>.eat()
println("Dog is eating")
}
}
</xmp>
<hr>
<p>Overriding Member Functions and variable</p>
<p>Just like Kotlin classes, members of a Kotlin class are also public and final by default.
<p>To allow a member function to be overridden, you need to mark it with the open modifier.</p>
<p>Moreover, The derived class that overrides a base class function must use the override modifier, otherwise, the compiler will generate an error</p>
</div>
<div id="InheritanceSecondary" class="tab-pane fade">
<h3>Inheritance with Primary and Secondary Constructors</h3>
<xmp>
fun main(args: Array<String>) {
var dog = TheDog("Black", "Pug")
}
open class TheAnimal { // Super class / Parent class / Base class
var color: String = ""
constructor(color: String) {
this.color = color
println("From Animal: $color")
}
}
class TheDog : TheAnimal { // Sub class / Child class / Derived class
var bread: String = ""
constructor(color: String, breed: String): super(color) {
this.bread = breed
println("From Dog: $color and $breed")
}
}
</xmp>
<hr>
<p>Inheritance with Primary and Secondary Constructors</p>
</div>
<div id="visibility" class="tab-pane fade">
<h3>Visibility Modifiers</h3>
<xmp>
open class Person { // Super class
private val a = 1
protected val b = 2
internal val c = 3
val a = 10
}
class Indian: Person() { // Sub class
fun printTest() {
print(a)
// a is not visible
// b, c, d are visible
}
}
class TestClass {
fun testing() {
var person = Person()
print(person.a)
// a, b are not visible
// c, d are visible
}
}
</xmp>
<hr>
<p>public always is visible</p>
<p>protected visible inside sub class, not visible inside other class</p>
<p>internal visible from module</p>
<p>private just visible from yourClass.kt</p>
</div>
<div id="abstract" class="tab-pane fade">
<h3>Abstract Class</h3>
<xmp>
fun main(args: Array<String>) {
// var person = MyPerson() // Not allowed. You cannot create instance of abstract class
var person = Indian() // Allowed. Abstract Super class reference variable
// pointing to child class object.
person.name = "Steve"
person.eat()
person.goToSchool()
}
abstract class MyPerson { // you cannot create instance of abstract class
abstract var name: String
abstract fun eat() // abstract properties are 'open' by default
open fun getHeight() {} // A 'open' function ready to be overridden
fun goToSchool() {} // A normal function
}
class Indian: MyPerson() {
override var name: String = "dummy_indian_name"
override fun eat() {
// Our own code
}
}
</xmp>
<hr>
<p>The Role or Abstract class is to just provide set of methods and properties</p>
<p>Abstract Class are Partlally defined class</p>
<p>Abstract Methods have no body when declared</p>
<p>Abstract Property cannot be initialized when declared</p>
<p>CONCLUSION</p>
<p>you cannot create intance/objects of ABSTRACT class</p>
<p>you need to override ABSTRACT methods, propertics inside Derived class</p>
</div>
<div id="interface" class="tab-pane fade">
<h3>Interface</h3>
<xmp>
fun main(args: Array<String>) {
var myButton = MyButton()
myButton.onTouch()
myButton.onClick()
}
interface MyInterfaceListener { // You cannot create the instance of interface
fun onTouch() // Methods in interface are abstract by default
fun onClick() { // Normal methods are public and open by default but NOT FINAL
println("MyInterfaceListener: onClick")
}
}
interface MySecondInterface { // You cannot create the instance of interface
fun onTouch() { // Normal Method
println("MySecondInterface: onTouch")
}
fun onClick() { // Normal methods are public and open by default but NOT FINAL
println("MySecondInterface: onClick")
}
}
class MyButton: MyInterfaceListener, MySecondInterface {
override fun onTouch() {
super<MyInterfaceListener>.onClick()
super<MySecondInterface>.onClick()
}
override fun onClick() {
super.onTouch()
}
}
</xmp>
<hr>
<p>Interface can contains both NORMAL methods and ABSTRACT methods</p>
<p>But they contain only ABSTRACT PROPERTY</p>
<p>Interface is not class</p>
<p>You can not create instance of an INTERFACE</p>
<p>similar to an ABSTRACT class</p>
</div>
<div id="data" class="tab-pane fade">
<h3>Data Clases</h3>
<xmp>
fun main(args: Array<String>) {
var user1 = User("Sam", 10)
var user2 = User("Sam", 10)
println(user1.toString())
if (user1 == user2)
println("Equal")
else
println("Not equal")
var newUser = user1.copy(id = 25)
println(newUser)
}
data class User(var name: String, var id: Int)
</xmp>
<hr>
<p>Any class contains functions such as:</p>
<p>equals():Boolean</p>
<p>hasCode():Int</p>
<p>toString:String</p>
<p>------------------</p>
<p>Kotlin creates a copy() too</p>
</div>
<div id="sealed" class="tab-pane fade">
<h3>Sealed</h3>
<xmp>
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
// the `else` clause is not required because we've covered all the cases
}
</xmp>
<hr>
<p>Sealed classes are used for representing restricted class hierarchies</p>
<p>when a value can have one of the types from a limited set, but cannot have any other type</p>
<p></p>
<p>They are, in a sense, an extension of enum classes:</p>
<p>- the set of values for an enum type is also restricted,</p>
<p>- but each enum constant exists only as a single instance</p>
<p>- whereas a subclass of a sealed class can have multiple instances which can contain state</p>
<a href="https://www.youtube.com/watch?v=P2ds5vTFIOw"><img src="static/youtube.png"></a>
<p></p>
</div>
</div>
</div>
</body>
</html>