gpt4 book ai didi

kotlin - kotlin 中的数据类继承?

转载 作者:行者123 更新时间:2023-12-05 08:50:22 30 4
gpt4 key购买 nike

我在 Java 中有 2 个模型类,其中一个扩展了另一个

@UseStag
public class GenericMessages extends NavigationLocalizationMap implements Serializable {
@SerializedName("loginCtaText")
@Expose
String loginCtaText;
@SerializedName("forgotPasswordCtaText")
@Expose
String forgotPasswordCtaText;

public String getLoginCtaText() {
return loginCtaText;
}

public String getForgotPasswordCtaText() {
return forgotPasswordCtaText;
}
}

NavigationLocalizationMap 类

public class NavigationLocalizationMap implements Serializable {

@SerializedName("localizationMap")
@Expose
public HashMap<String, LocalizationResult> localizationMap;

@SerializedName("geoTargetedPageIds")
@Expose
public HashMap<String, String> geoTargetedPageIdsMap;

@SerializedName("pageId")
@Expose
public String pageId;

public HashMap<String, LocalizationResult> getLocalizationMap() {
return localizationMap;
}

public void setLocalizationMap(HashMap<String, LocalizationResult> localizationMap) {
this.localizationMap = localizationMap;
}

public HashMap<String, String> getGeoTargetedPageIdsMap() {
return geoTargetedPageIdsMap;
}

public void setGeoTargetedPageIdsMap(HashMap<String, String> geoTargetedPageIdsMap) {
this.geoTargetedPageIdsMap = geoTargetedPageIdsMap;
}

public void setPageId(String pageId) {
this.pageId = pageId;
}

public String getPageId() {
String countryCode = !TextUtils.isEmpty(CommonUtils.getCountryCode()) ? CommonUtils.getCountryCode() : Utils.getCountryCode();
String _pageId = null;
if (getGeoTargetedPageIdsMap() != null) {
_pageId = getGeoTargetedPageIdsMap().get(countryCode);
if (_pageId == null) {
_pageId = getGeoTargetedPageIdsMap().get("default");
}
}
if (_pageId == null) {
_pageId = pageId;
}
return _pageId;
}

}

所以我正在将当前代码库转换为 Kotlin如果我将它们创建为数据类,我将无法继承。有替代方案吗?

最佳答案

使用界面

interface Human {
val name: String
}

data class Woman(override val name: String) : Human

data class Mom(override val name: String, val numberOfChildren: Int) : Human

使用密封类

sealed class Human {
abstract val name: String
}

data class Woman(override val name: String) : Human()

data class Mom(override val name: String, val numberOfChildren: Int) : Human()

关于kotlin - kotlin 中的数据类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62318832/

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