|
@@ -0,0 +1,77 @@
|
|
|
|
|
+package com.virtualprogrammers.learning.kotlin
|
|
|
|
|
+
|
|
|
|
|
+data class Customer(
|
|
|
|
|
+ val name: String,
|
|
|
|
|
+ val address: String,
|
|
|
|
|
+ var age: Int
|
|
|
|
|
+) {
|
|
|
|
|
+ constructor(name: String, age: Int) : this(name, "", age) {
|
|
|
|
|
+ println("second constructor")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ init {
|
|
|
|
|
+ println("init block")
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class AlternativeCustomer(val name: String, val age: Int) {
|
|
|
|
|
+ var address: String
|
|
|
|
|
+
|
|
|
|
|
+ init {
|
|
|
|
|
+ address = ""
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ constructor(name: String, address: String, age: Int) : this(name, age) {
|
|
|
|
|
+ this.address = address
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class AnotherAlternativeCustomer(val name: String, val address: String = "", var age: Int) {
|
|
|
|
|
+ var approved: Boolean = false
|
|
|
|
|
+ set(value) {
|
|
|
|
|
+ if (age >= 21) {
|
|
|
|
|
+ field = value
|
|
|
|
|
+ } else {
|
|
|
|
|
+ println("no can do")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ val nextAge
|
|
|
|
|
+ get() = age + 1
|
|
|
|
|
+
|
|
|
|
|
+ operator fun component1() = name
|
|
|
|
|
+ operator fun component2() = age
|
|
|
|
|
+
|
|
|
|
|
+ fun upperCaseName() = name.toUpperCase()
|
|
|
|
|
+
|
|
|
|
|
+ override fun toString() = "$name $address $age"
|
|
|
|
|
+
|
|
|
|
|
+ companion object {
|
|
|
|
|
+ fun getInstance() = AnotherAlternativeCustomer("micky", "address", 22)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fun main() {
|
|
|
|
|
+ val customer = Customer("leon", "waterlelie", 45)
|
|
|
|
|
+ customer.age = 20
|
|
|
|
|
+ val customer2 = Customer("John", 49)
|
|
|
|
|
+ println("${customer.name} is ${customer.age}")
|
|
|
|
|
+ println("${customer2.name} is ${customer2.age}")
|
|
|
|
|
+
|
|
|
|
|
+ val customer3 = AnotherAlternativeCustomer("leon", "waterlelie", 20)
|
|
|
|
|
+ customer3.approved = true
|
|
|
|
|
+ println("${customer3} == ${customer3.name} is ${customer3.age}")
|
|
|
|
|
+ customer3.nextAge
|
|
|
|
|
+ println("${customer3.name.uppercase()} is ${customer3.nextAge}")
|
|
|
|
|
+
|
|
|
|
|
+ val customer4 = AnotherAlternativeCustomer.getInstance()
|
|
|
|
|
+ println(customer4)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ val customer5 = Customer("Sally", 29)
|
|
|
|
|
+ println(customer5)
|
|
|
|
|
+ val customer6 = customer5.copy(name = "Diana")
|
|
|
|
|
+ println(customer6)
|
|
|
|
|
+
|
|
|
|
|
+ val (name, address, age) = customer6
|
|
|
|
|
+}
|