Kotlin的類別中可以宣告可變(var)或唯讀(val)的屬性
class Address {
var name: String = ...
var street: String = ...
var city: String = ...
var state: String? = ...
var zip: String = ...
}
使用類別中的屬性與Java引用方式相似
fun copyAddress(address: Address): Address {
val result = Address()
result.name = address.name
result.street = address.street
// ...
return result
}
property的引用其實是對getter及setter方法做訪問
var <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]
initializer跟geter及setter是可選的 var是允許有getter及setter方法
val不可以有setter方法