[英]How to get copy of the ArrayList<Sealed Class> in android using kotlin
android中封存的class的arraylist副本如何獲取
private var homePageApiResponseList: ArrayList<HomeApiResponseModel> = ArrayList()
這里的 HomeApiResponseModel 是一個 Sealed class。HomeApiResponseModel 給出如下
sealed class HomeApiResponseModel {
data class HomeCategoryListModel(
var categoryList : MutableList<CategoryModel> = mutableListOf(),
var categoryNameType : String = ""
) : HomeApiResponseModel()
data class HomeBestSellerListModel(
var bestSellerList : MutableList<ChildrenModel> = mutableListOf(),
var bestSellerNameType : String = ""
) : HomeApiResponseModel()
data class HomeMustTryListModel(
var mustTryList : MutableList<ChildrenModel> = mutableListOf(),
var mustTryNameType : String = ""
) : HomeApiResponseModel()
}
通常 object 副本的 arraylist 很容易通過anyList.map { it.copy() }
在密封的 class 中顯示錯誤。 如何獲得密封class的arraylist副本
謝謝
在父類中創建一個摘要 function。 每個孩子都可以實現它並調用他們自己的copy()
。 摘要 function 的名稱應與“copy”不同,以避免沖突。
順便說一句,在您的情況下,密封接口可能比密封 class 更干凈,因為子級之間沒有共同的功能。 我建議避免將可變 collections 與var
屬性結合使用。 以兩種不同的方式使某些東西可變會增加(通常是不必要的)復雜性和更多出現錯誤的機會。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.