我实现了一个带有协变/逆变类型约束的接口(interface),编译器告诉我 '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(); }
}
问题是您也将 Mail
声明为通用的 - 您已将 Student
和 MatureStudent
类型参数。你只想:
class Mail : IMail<Student,MatureStudent> {
我是一名优秀的程序员,十分优秀!