2017-06-06 36 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

相關問題