【问题标题】:Kotlin: how to use multiple Generic in Class?Kotlin:如何在类中使用多个泛型?
【发布时间】:2017-06-06 14:19:54
【问题描述】:

我正在尝试将此 c# 类转换为 kotlin for android:

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”行。

【问题讨论】:

标签: android kotlin


【解决方案1】:

在 Kotlin 中,您可以在声明时指定类型参数的上限:

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

如果您有多个上限,则应分别使用where 指定它们,请参阅another Q&A

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

【讨论】:

  • 谢谢,你救了我!删除“类”字使其工作:-)
  • 老兄,很难找到这些信息。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多