在下面的代碼中,我想對其進行概括,因此我可以代替viewBinding.editText.text并且viewModel.property.price可以對 egviewBinding.secondEditText.text和使用相同的方法 viewModel.property.income。
我正在考慮交換viewBinding.editText.text在主建構式中定義的變數,但隨后我需要該變數包含對viewBinding.editText.text/viewBinding.secondEditText.text等的參考,而不是包含一個值。
這可能嗎?我已經為此查看了長度,但找不到任何有用的東西。
fun updateProperty() {
//... other irrelevant code
if (viewBinding.editText.text.toString() != "") {
viewModel.property.price = viewBinding.editText.text.toString().toDouble()
}
//... other irrelevant code
}
uj5u.com熱心網友回復:
您可以將引數傳遞給函式,是的!
這是最簡單的:
fun updateProperty(editText: EditText) {
val contents = editText.text.toString()
}
很簡單,你只要傳入 an 的任何實體,EditText函式就用它做一些事情。
如果你只是使用物件與getter和setter方法,你可以定義型別,你打算使用,并通過他們進來依據是什么viewmodel.property,就是你可能能夠通過,在為好,并獲得price與income上它。如果您想使用其他型別,也許可以使用介面或密封類 - 如果您要使用適用于所有型別的通用函式,則它們需要一些共性。
屬性有點棘手 - 假設viewmodel.property包含 a var price: Double,并且您不想傳遞property本身,只是Double存在于某處的 a ,您可以這樣做:
import kotlin.reflect.KMutableProperty0
var wow: Double = 1.2
fun main() {
println(wow)
setVar(::wow, 6.9)
println(wow)
}
fun setVar(variable: KMutableProperty0<Double>, value: Double) {
variable.set(value)
}
>> 1.2
>> 6.9
(如果您不熟悉語法,請參閱屬性參考::)
KMutableProperty0表示var對沒有任何接收器的可變屬性 (a )的參考- 只是一個基本的 var。不要擔心reflect匯入,這是基本的反射內容,如函式參考,它是基本 Kotlin 安裝的一部分
uj5u.com熱心網友回復:
是的,方法引數也可以是對類或介面的參考。并且方法引數也可以是對其他方法/函式/lambda 的參考。
如果您正在處理難以概括的情況,請考慮使用某種控制反轉(函式作為引數或 lambda)。
您將 lambda 引數添加到 updateProperty 函式中
fun updateProperty(onUpdate: (viewBinding: YourViewBindingType, viewModel: YourViewModelType) -> Unit) {
//... other irrelevant code
// here you just call the lambda, with any parameters that might be useful 'on the other side'
onUpdate(viewBinding, viewModel)
//... other irrelevant code
}
代碼中的其他地方 - 案例 1:
updateProperty() { viewBinding, viewModel ->
if (viewBinding.editText.text.toString() != "") {
viewModel.property.price = viewBinding.editText.text.toString().toDouble()
}
}
代碼中的其他地方 - 案例 2:
updateProperty() { viewBinding, viewModel ->
if (viewBinding.secondEditText.text.toString() != "") {
viewModel.property.income = viewBinding.secondEditText.text.toString().toDouble()
}
}
代碼中的其他地方 - 案例 3:
updateProperty() { viewBinding, viewModel ->
// I am a totally different case, because I have to update two properties at once!
viewModel.property.somethingElse1 = viewBinding.thirdEditText.text.toString().toBoolean()
viewModel.property.somethingElse2 = viewBinding.fourthEditText.text
.toString().replaceAll("[- ]*", "").toInt()
}
然后,您可以更進一步,為前 2 種情況定義一個函式,因為這 2 種情況可以泛化,然后在 lambda 中呼叫它(或者甚至將其作為 lambda 傳遞),這將為您節省一些代碼,如果您可以updateProperty()在代碼中的許多地方呼叫,或者只是為每個地方定義一個簡單的函式,然后呼叫它,就像這樣
fun updatePrice() = updateProperty() { viewBinding, viewModel ->
if (viewBinding.editText.text.toString() != "") {
viewModel.property.price = viewBinding.editText.text.toString().toDouble()
}
}
fun updateIncome() = updateProperty() { viewBinding, viewModel ->
if (viewBinding.secondEditText.text.toString() != "") {
viewModel.property.income = viewBinding.secondEditText.text.toString().toDouble()
}
}
然后在代碼中的其他地方,您只需以非常簡單的方式呼叫它
updatePrice()
updateIncome()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/385823.html
