class Customer (var name: String) // 1
class Contact(val id: Int, var email: String) // 2
fun main() {
val customer = Customer("Alex")
val contact = Contact(1, "mary@gmail.com") // 4
println(customer.name) // 5
println(contact.id) // 5
println(contact.email) // 5
}
Alex 1 mary@gmail.com
- Declares a class named
Customerwithout any properties or user-defined constructors. A non-parameterized default constructor is created by Kotlin automatically. - Declares a class with two properties: immutable
idand mutableemail, and a constructor with two parametersidandemail. - Creates an instance of the class
Customervia the default constructor. Note that there is nonewkeyword in Kotlin. - Creates an instance of the class
Contactusing the constructor with two arguments. - Accesses the property
id. - Updates the value of the property
email.
沒有留言:
張貼留言