Kotlin:如何在类中使用多个Generic?
我试图将这个C#类转换为Android的kotlin:
public class ChildItemCollection : ICollection where P : class where T : ChildItem
where C : class, ICollection { private P _parent; private C _collection; public ChildItemCollection(P parent, C collection) { this._parent = parent; this._collection = collection; } ... } public class ChildItemCollection
: ChildItemCollection<P, T, List> where P : class where T : ChildItem
{ #region Constructors public ChildItemCollection(P parent) : base(parent, new List()) { } public ChildItemCollection(P parent, ICollection collection) : this(parent) { foreach (T item in collection) this.Add(item); } #endregion Constructors }
我尝试了很多事情,但都没有成功。
我不明白如何使用“where”行。
在Kotlin中,你可以在声明中指定types参数的上限:
class ChildItemCollection, C : Collection> : ...
如果你有多个上限,他们应该与where
分开指定,参见另一个问答 。
此外,Kotlin没有class
上限,因为值types和类之间没有区别。
- 如何在values / ids.xml中获得自定义ID的引用
- 为什么不在Kotlin的Android 4.x下运行应用程序?
- Android / Kotlin使用Java.io.File读取文件:路径有问题
- 检查我的活动是否在MultiWindowMode或不使用Kotlin
- Retrofit2 + Kotlin中的SimpleXML:MethodException:注释必须标记一个set或get方法
- 在Kotlin中使用FirebaseListAdapter
- 将数据显示从Java转换为Kotlin
- 在Android Studio中将代码从Java转换为Kotlin
- (未知来源:7)kotlin android中的错误