shjanken
9/11/2017 - 5:46 AM

[Kotlin in action] <kotlin in action> study code #kotlin #study

[Kotlin in action] study code #kotlin #study

// 因为 throws, return 在 kotlin 中都是表达式,所以可以直接放在 ?: 后面使用

class Address(val streetAddress: String, val zipCode: Int, val city: String, val country: String)

class Company(val name: String, val address: Address?)

class Person(val name: String, val company: Company?)

fun printShippingLabel(person: Person) {
    val address = person.company?.address ?:
            throw IllegalArgumentException("No address") // 如果有问题就直接报错
    with(address) {
        println(streetAddress)
        println("$zipCode $city, $country")
    }
}