[英]Does function parameter behaves like a val type of variable in Kotlin?
我正在創建一個函數,並嘗試增加聲明為Integer的參數的值。 但是它說不能重新分配該參數的值。 我是一個初學者,所以如果我缺少一些概念,請告訴我。
是的,在Kotlin函數中無法更改參數。
(但是,如果它們引用可變對象,則這些對象可以更改。例如,如果您聲明fun a(b: List<String>) ,則可以向列表中添加另一個String;但是不能設置b以引用其他列表。)
fun a(b: List<String>)
b
這不同於Java,在Java中參數是可變的(除非指定為final )。
final
這種行為是在Kotlin里程碑M5.1中宣布的 ,該行為被解釋為避免混淆(尤其是在構造函數中),並促進了良好的風格。
另請參閱此答案。
[英]passing 'val' type to a function
[英]kotlin handling variable return type of function
[英]Specifying function parameter type, but not variable
[英]Parameter passed by a reference behaves like a copy
[英]Function parameter names in Kotlin
[英]PHP public function behaves like static
[英]Use a parameter switch to change how a function behaves
[英]How to insert a variable type as a parameter function?
[英]How to assign a null value to a function type variable in Kotlin?
[英]Passing Function Type As Template Parameter Does Not Compile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.