gpt4 book ai didi

android - Android kotlin方法接受任何对象类型作为参数

转载 作者:行者123 更新时间:2023-12-02 13:01:45 25 4
gpt4 key购买 nike

我想将2种不同的对象类型传递给一种方法来更新 View 。我怎么有这种方法
接受2种不同的对象类型并访问它们,而不是对2种不同的对象类型使用2种不同的方法。

我需要这样的东西-

fun updateView(object: Any<T>) {
//Access the objects here to update the view
}

最佳答案

fun <T : Any> updateView(obj: T) {
//Access the objects here to update the view
}

要么
fun updateView(obj: Any ?= null, obj2:Any ?= null) {
// Access the objects here to update the view

// pass check nullity and use which you want (or not null), other parameter will remain null

obj?.let {
it...
}

obj2?.let {
it...
}
}

call
updateView(obj1, obj2)

// OR

updateView(obj2 = myObj2)

关于android - Android kotlin方法接受任何对象类型作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60449001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com