2017-06-06 19 views
-1

我试图转换此C#类科特林为Android:Kotlin:如何在类中使用多个Generic?

public class ChildItemCollection<P, T, C> : ICollection<T> 
    where P : class 
    where T : ChildItem<P> 
    where C : class, ICollection<T> 
{ 

    private P _parent; 
    private C _collection; 

    public ChildItemCollection(P parent, C collection) 
    { 
     this._parent = parent; 
     this._collection = collection; 
    } 


... 

} 

public class ChildItemCollection<P, T> : ChildItemCollection<P, T, List<T>> 
     where P : class 
     where T : ChildItem<P> 
    { 
     #region Constructors 
     public ChildItemCollection(P parent) 
      : base(parent, new List<T>()) { } 

     public ChildItemCollection(P parent, ICollection<T> collection) 
      : this(parent) 
     { 
      foreach (T item in collection) 
       this.Add(item); 
     } 
     #endregion Constructors 
    } 

我已经尝试了许多事情没有成功。

我不明白如何使用“where”行。

+0

定义'没有成功'。 – nhaarman

+0

'其中P:class'看起来不对,'class'是关键字。看看最后一个代码示例:https://kotlinlang.org/docs/reference/generics.html#upper-bounds –

+0

请在您的问题中提供确切的编译错误(这可能是为什么所有的投票都发生了) – voddan

回答

2

在科特林,你可以在他们的声明中指定的类型参数上界:

class ChildItemCollection<P, T : ChildItem<P>, C : Collection<T>> : ... 

如果您有多个上限,应分别与where规定,请参阅another Q&A

另外,Kotlin没有class上限,因为值类型和类之间没有区别。

+0

谢谢,你救了我!删除“class”这个词让它起作用:-) – SWAppDev

相关问题