kotlin - Kotlin 中的多重赋值
问题描述
我可以声明和初始化a
,b
就像这样
var (a, b) = Pair(1, 2)
但我不能像这样重新分配它们
(a, b) = Pair(3, 4)
这怎么不可能?我错过了什么吗?
解决方案
那是因为(a, b)
是 2 个单独的变量,您可以通过componnetN()
函数进行初始化。
您可以使用 a 和 b 作为不同的变量。如果你有对,你必须这样写var a = Pair(2, 1)
或者可以使用 vara = 1 to 2
在这里阅读更多信息
https://kotlinlang.org/docs/destructuring-declarations.html
推荐阅读
- azure-devops - 有关新测试失败的 Azure 管道电子邮件通知
- vue.js - 输入占位符值莫名其妙地移动到其父元素
- python - 在将数据导入 MySQL 之前,在 Python 中的数据清理活动期间将所有反斜杠“\”替换为前斜杠“/”
- flutter - 如何在flutter中自定义水平时间选择器?
- laravel - Wordpress 面临的帖子名称永久链接问题无法正常工作,但在将网站迁移到 live aws elastic beanstalk 后,普通链接正在工作
- android - 我可以做什么?在这个错误中 Fastlane [!] Google Api Error: Invalid request - UploadException:ADHOC_PACKAGE_NAME_MISMATCH [[]]
- fortify - 强化报告生成器
- php - Woocommerce - 获取子类别的父类别名称
- reactjs - 如何从 React 中的对象数组中打字
- flutter - 如何使用 PaginatedDataTable 更改列的背景颜色