我不知道如何解决通用接口的问题。
通用接口代表对象的工厂:
interface IFactory<T>
{
// get created object
T Get();
}
接口代表计算机工厂(计算机类)指定通用工厂:
interface IComputerFactory<T> : IFactory<T> where T : Computer
{
// get created computer
new Computer Get();
}
通用接口代表对象的特殊工厂,可克隆(实现接口 System.ICloneable):
interface ISpecialFactory<T> where T : ICloneable, IFactory<T>
{
// get created object
T Get();
}
Class 代表计算机(Computer 类)和可克隆对象的工厂:
class MyFactory<T> : IComputerFactory<Computer>, ISpecialFactory<T>
{
}
我在 MyFactory 类中收到编译器错误消息:
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'exer.IFactory<T>'.
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.ICloneable'.