-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes (secondary constructors).kt
74 lines (45 loc) · 1.78 KB
/
classes (secondary constructors).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
/*
Secondary constructors declare parameters and not properties
they can be used when an object doesn't make use of all the
attributes of its class
*/
/**
* The secondary constructor calls the values of the primary constructor
* It is declared using the constructor() method
* */
fun main (){
val user = Userrs("Mary", "Anne", 30)
val user2 = Userrs("Che", "Bih")
val user3 = Userrs("Paul")
println("Name : ${user.name}")
println("lastname : ${user.lastname}")
println("age : ${user.age}")
println("\n")
println("Name : ${user2.name}")
println("lastname : ${user2.lastname}")
println("age : ${user2.age}")
println("\n")
println("Name : ${user3.name}")
println("lastname : ${user3.lastname}")
println("age : ${user3.age}")
var myPhone = phone("Oppo", 4, 32)
myPhone.DisplayParameters()
}
class Users(var name : String, var lastname : String, var age : Int){
/*The secondary constructor takes parameters and calls properties of the primary constructor
*/
//object created takes only name as attribute
constructor(name: String) : this(name, "LastName", 0){
println("first secondary constructor") // a secondary constructor can have some code in it
}
//object will have only first and last names, no age.
constructor(name: String, lastname : String) : this(name, lastname, 0){
println("Second secondary constructor")
}
}
class phone(var Brand : String, var RAM : Int, var storageSpace : Int){
constructor(Brand : String, RAM : Int) : this(Brand, RAM, 0)
fun DisplayParameters(){
print("Characteristics : Brand : ${this.Brand}, RAM : ${this.RAM}, Storage : ${this.storageSpace}")
}
}