kotlin - 接口中不允许使用属性初始化器
问题描述
我的 Java 项目有一个具有给定值的 ItemType 接口,该接口由某些类实现。如何在 Kotlin 上实现这个接口?
public interface ItemType {
int TYPE_OPTION = 2;
int TYPE_GRID = 3;
int TYPE_CAROUSEL = 4;
int TYPE_MUSIC = 5;
int TYPE_GUESS = 6;
int getItemType();
}
解决方案
您可以使用companion object
:
interface ItemType {
val itemType: Int
companion object {
const val TYPE_OPTION = 2
const val TYPE_GRID = 3
const val TYPE_CAROUSEL = 4
const val TYPE_MUSIC = 5
const val TYPE_GUESS = 6
}
}
推荐阅读
- apache-spark - 如何通过 Spark 运行 HDFS 文件系统命令?
- python - 在python中记录所有http请求
- node.js - EJS条件导入不显示html
- java - 2.1 企业 bean 上的资源引用标记
- python-3.x - 如何使用 asyncio 和 aiohttp 进行循环而不是 for 循环?
- wordpress - 关闭无限滚动
- javascript - aync await 是否与 React setState 方法一起使用?
- kubernetes - 卡夫卡内存泄漏
- python-3.x - Pandas,字符串 case 语句到数据框 - 两个以上的条件
- for-loop - 如何处理 CTE 中的所有记录