美文网首页
[日更][26]-Kotlin

[日更][26]-Kotlin

作者: 代码多哥 | 来源:发表于2023-09-05 14:10 被阅读0次

因为时间很短,所以内容不是很复杂,写一个有价值的小知识,主要是为了保持每日学习和写作的习惯,大作还是会写到相关的主题里面,尽量做到周更。敬请关注本人的主要作品集:

一路向下之AOSP研究

为了能够最大限度的保证文章的质量,日更主要采用翻译的方法来完成。本系列将主要翻译Kotlin官网的内容。具体的地址

https://kotlinlang.org/docs/home.html

二十五, 函数-类-属性访问

若要访问实例的属性,请在实例名称后面加上句点 . 然后写入该属性的名称。:

class Contact(val id: Int, var email: String)

fun main() {
    val contact = Contact(1, "mary@gmail.com")
    
    // Prints the value of the property: email
    println(contact.email)           
    // mary@gmail.com

    // Updates the value of the property: email
    contact.email = "jane@gmail.com"
    
    // Prints the new value of the property: email
    println(contact.email)           
    // jane@gmail.com
}

要将属性的值连接为字符串的一部分,可以使用字符串模板($)。例如:

println("Their email address is: ${contact.email}")

相关文章

网友评论

      本文标题:[日更][26]-Kotlin

      本文链接:https://www.haomeiwen.com/subject/oprxvdtx.html