例如在Java中我可以写:
public abstract class Element<S extends Snapshot> { ... }
public abstract class Snapshot<E extends Element> { ... }
Run Code Online (Sandbox Code Playgroud)
然后,在某处,扩展这些类:
public class SnapshotImpl extends Snapshot<ElementImpl> { ... }
public class ElementImpl extends Element<SnapshotImpl> { ... }
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在Kotlin中实现相同的类层次结构时:
abstract class Element<S : Snapshot>
abstract class Snapshot<E : Element>
Run Code Online (Sandbox Code Playgroud)
我遇到了以下编译错误:
Error:(6, 28) Kotlin: One type argument expected for class Snapshot<E> defined in model
Error:(6, 25) Kotlin: One type argument expected for class Element<S> defined in model
有没有办法在Kotlin中重现相同类型的参数限制?
hot*_*key 11
Kotlin没有原始类型,你不能只删除类型参数.
类似于原始类型的一个选项是使用星形投影:
abstract class Element<S : Snapshot<*>> { /* ... */ }
abstract class Snapshot<E : Element<*>> { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
但是您将无法正常使用类型参数泛型成员.
另一个选择是引入这样的相互约束:
abstract class Element<E : Element<E, S>, S : Snapshot<S, E>>() { /* ... */ }
abstract class Snapshot<S : Snapshot<S, E>, E : Element<E, S>>() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
使用此定义,您可以确定,如果您定义SomeSnapshot: Snapshot<SomeSnapshot, SomeElement>,则类型SomeElement 可以识别 SomeSnapshot,因为它被约束为派生自Element<SomeElement, SomeSnapshot>.
那么实现将是:
class SomeElement : Element<SomeElement, SomeSnapshot>() { /* ... */ }
class SomeSnapshot : Snapshot<SomeSnapshot, SomeElement>() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
968 次 |
| 最近记录: |