[英]Java Equivalent of Kotlin Sealed Class Type Detection
I want to access information from this Kotlin class in Java. It is being imported via a Gradle library.我想从 Java 中的这个 Kotlin class 访问信息。它是通过 Gradle 库导入的。
Sealed Class:密封Class:
public sealed class Resource<in T> private constructor() {
public final data class Error public constructor(exception: kotlin.Throwable, statusCode: kotlin.Int?) : #.response.Resource<kotlin.Any> {
public final val exception: kotlin.Throwable /* compiled code */
public final val statusCode: kotlin.Int? /* compiled code */
public final operator fun component1(): kotlin.Throwable { /* compiled code */ }
public final operator fun component2(): kotlin.Int? { /* compiled code */ }
}
public final data class Success<T> public constructor(data: T) : com.tsfrm.commonlogic.response.Resource<T> {
public final val data: T /* compiled code */
public final operator fun component1(): T { /* compiled code */ }
}
}
In Java, I'm trying to determine whether it is of type Success or Error, and if possible, I want to be able to retrieve the "statusCode" from it.在 Java 中,我试图确定它是 Success 还是 Error 类型,如果可能的话,我希望能够从中检索“statusCode”。 I know in Kotlin it'd be preferable to use the 'when' logic in Kotlin, but I haven't been able to find a suitable replacement for it.
我知道在 Kotlin 中最好使用 Kotlin 中的“when”逻辑,但我一直没能找到合适的替代品。
The best you can do in Java is你在 Java 中能做的最好的事情是
public <T> void doResourceThing(Resource<T> r) {
if (r instanceof Success) {
Success<T> success = (Success<T>) r;
...
} else if (r instanceof Error) {
Error err = (Error) r;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.