-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9. OOP.kt
executable file
·64 lines (56 loc) · 2.66 KB
/
9. OOP.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
package za.soupcookie.kotlin
fun main() {
var jack = Person("Jack", "Pamoemba")
var john = Person("John")
// john.health = 5 // not allowed, since health has a private setter
println(john.health)
john.age = 20 // you don't have to create a getAge or setAge, kotlin does it behind the scenes
println(john.age)
var nick = Person("Nick", "Peere", 23) // there are 2 different constructors
println(nick.age)
nick.isWho()
}
// class Person constructor(firstName: String, lastName: String)
// the above is the exact same as the below
class Person(firstName: String, lastName: String = "Macros") { // create a basic class
// firstName and lastName automatically have this.firstName = firstName, so no need
// to implement it
// lastName has a default value
var age: Int? = null // can create member variables
// creating getters and setters are optional in kotlin, unless you want to
// modify what happens when getting or setting a value
get(){ // this is a getter in kotlin
// now whenever we get the age, the below will print out
println("Getter for age was called!")
// field = this.age
return field ?: 0
}
set(value){ // this is a setter in kotlin
// field = this.age
// value is the value passed
field = value
}
var health: Int = 100
private set // makes the setter private, so only THIS class can use it
var firstName: String? = null
var lastName: String? = null
// you can create a 2nd constructor, to make the age variable an optional input
// this(firstName, lastName) = the original constructor still gets called, the below
// will basically use that constructors data and pass it in here
constructor(firstName: String, lastName: String, age: Int): this(firstName, lastName){
this.age = age // specify what constructor should do
println("This constructor will get called as well! They are $age years old!")
}
init { // basically the constructor, gets ran as soon as object is created
this.firstName = firstName
this.lastName = lastName
this.health -= 50 // can only be set from inside the class, since the setter is private
// we don't have to specify this here, since whatever is passed into the constructor
// is available in the init, as is
println("This is the init function for $firstName $lastName")
}
fun isWho(){
// we use ${} since if we used $ only 'this' would've been registered
println("${this.firstName} with the last name of ${this.lastName} is ${this.age} years old")
}
}