【问题标题】:Inheritance from generic interfaces从泛型接口继承
【发布时间】:2013-03-22 11:17:11
【问题描述】:

我不知道如何解决泛型接口的问题。

通用接口代表对象的工厂:

interface IFactory<T>
{
    // get created object
    T Get();    
}

Interface代表计算机工厂(Computer class)指定通用工厂:

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'.  

【问题讨论】:

    标签: c# generics inheritance interface


    【解决方案1】:

    不确定这是否是一个错字,但应该这样:

    interface ISpecialFactory<T>
            where T : ICloneable, IFactory<T>
    

    真的

    interface ISpecialFactory<T> : IFactory<T>
            where T : ICloneable
    

    真的,我认为这可能是你想要做的:

    public class Computer : ICloneable
    { 
        public object Clone(){ return new Computer(); }
    }
    
    public interface IFactory<T>
    {
        T Get();    
    }
    
    public interface IComputerFactory : IFactory<Computer>
    {
        Computer Get();
    }
    
    public interface ISpecialFactory<T>: IFactory<T>
        where T : ICloneable
    {
        T Get();
    }
    
    public class MyFactory : IComputerFactory, ISpecialFactory<Computer>
    {
        public Computer Get()
        {
            return new Computer();
        }
    }
    

    现场示例:http://rextester.com/ENLPO67010

    【讨论】:

    • 感谢您的回答,但这并不是我想要实现的。您能否将其更改为 MyFactory 生成所有可克隆对象,而不仅仅是“计算机”对象?
    • @Matt - 抱歉,这没有多大意义。如果工厂没有具体化,它怎么知道如何去建造东西?即使在您的原始帖子MyFactory 中实现了IComputerFactory。你应该用更多关于你想要实现的目标来更新你的问题
    • 如果工厂没有具体化,它怎么知道如何建造东西?我想使用泛型。我的原始帖子 MyFactory 实现了 IComputerFactory 接口和 ISpecialFactory 接口。这意味着 MyFactory 不仅能够生成 Computer 类的实例,还能够生成所有可克隆类的实例。我认为 myFactory 类中的新方法是可能的,比如 public T Get {return new T()}。不知道有没有可能……
    • 所以你想让MyFactory 既构造Computer 又构造any ICloneable?似乎有点奇怪,但好的,让我重新审视我的答案
    • 是的,完全正确。我知道,这很奇怪。我想知道这是否可能。
    【解决方案2】:

    我猜你对ISpecialFactory&lt;T&gt; 的定义不正确。改成:

    interface ISpecialFactory<T> : IFactory<T>
        where T : ICloneable
    {
        // get created object
        T Get();
    }
    

    您可能不希望 T 类型实现 IFactory&lt;T&gt;

    【讨论】:

      【解决方案3】:

      试试这个代码块:

      class MyFactory<T> : IComputerFactory<Computer>, ISpecialFactory<T>
          where T: ICloneable, IFactory<T>
          {
      
          }
      

      【讨论】:

      • 这需要T 实现IFactory&lt;T&gt;
      猜你喜欢
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2013-10-04
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多