1

我已经实现了一个带有协/逆变类型约束的接口,编译器告诉我'Student' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'UserQuery.IMail<T,U>'

从我所见,我满足了这些要求。我究竟做错了什么?

class Person { public Person(){} }
class Student : Person { public Student(){} }
class MatureStudent : Student {public MatureStudent(){}}

interface IMail<in T, out U> where T : new() where U : new() {
    void Receive(T t);
    U Return();
}

class Mail<Student,MatureStudent> : IMail<Student,MatureStudent> {
    public void Receive(Student s) {}
    public MatureStudent Return() { return new MatureStudent(); }
}
4

1 回答 1

7

问题是您也声明Mail是通用的 - 您已经制作StudentMatureStudent输入了参数。你只想:

class Mail : IMail<Student,MatureStudent> {
于 2013-08-13T12:14:31.123 回答