35 安全访问
?. 只在接收者存在时读取成员。
norm
class Task {
String title
}
main() {
Task? missing = null
Task? present = Task(title: "Learn Norm")
printLine(missing?.title == null)
printLine(present?.title)
}预期输出:
text
true
Learn Norm对 missing,missing?.title 直接得到 null,不读取字段;对 present,它得到任务标题。整个表达式仍是可空的。
动手试试:再加入一个可空成员,连续使用两次安全访问。
详细规则:参考。