如何使用相同的接口与diferrent模板参数两次,在一个界面? [英] How to use same interface two times with diferrent template parameters, in an interface?
本文介绍了如何使用相同的接口与diferrent模板参数两次,在一个界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为这将是更清晰的这个例子。我们要看到两种方法在处理器类diferrent参数。
诠释过程(int值);
双联工艺(double值);
但是编译器说,对IRoot:
'Generics.IRoot'不能同时实现Generics.IProcess 。'和'Generics.IProcess,因为它们可以结合一些类型参数替换
公共类处理器:IRoot< INT,双,INT,双>
{
//这里我们要2种方法
公众诠释过程(INT项){}
公共双联工艺(双项){}
}
酒店的公共接口IProcess< TResult,TItem>
{
TResult过程(TItem项目);
}
公共接口IRoot< TR1,TR2,TItem1,TItem2> :
IProcess< TR1,TItem1>中
IProcess< TR2,TItem2>
{
}
解决方案
您很可能使用这种实现的。你会失去一些通用的参数:
公共接口IBase的< TM,TPkey>
,其中TM:BTYPE
式TPkey:新的()
{
TM获取(TPkey键);
}
公共接口IABase< TPK> :IBase的< ConcreteTmA,TPK> {}
公共接口IBBase< TPK> :IBase的< ConcreteTmB,TPK> {}
公共类根< TPK> :
IABase< TPK>中
IBBase< TPK>
,其中TM:M型
其中TPM:PMTYPE
,其中TPK:新的()
{
ConcreteTmA IABase.Get(TPK键)
{
}
ConcreteTmB IBBase.Get(TPK键)
{
}
}
I Think it would be more clearer with this example. We Want to see two methods with diferrent parameters in the processor class. "int Process (int value);" "double Process (double value);"
But compiler says for IRoot : 'Generics.IRoot' cannot implement both 'Generics.IProcess' and 'Generics.IProcess' because they may unify for some type parameter substitutions.
public class Processor : IRoot<int, double, int, double>
{
// Here we want 2 methods
public int Process(int item) { }
public double Process(double item) { }
}
public interface IProcess<TResult, TItem>
{
TResult Process(TItem item);
}
public interface IRoot<TR1, TR2, TItem1, TItem2> :
IProcess<TR1, TItem1>,
IProcess<TR2, TItem2>
{
}
解决方案
You could probably use this kind of implementation. You'll loose some generic arguments:
public interface IBase<TM, TPkey>
where TM : bType
where TPkey : new ()
{
TM Get(TPkey key);
}
public interface IABase<TPK> : IBase<ConcreteTmA, TPK> {}
public interface IBBase<TPK> : IBase<ConcreteTmB, TPK> {}
public class Root <TPK> :
IABase<TPK>,
IBBase<TPK>
where TM : MType
where TPM : PMType
where TPK : new()
{
ConcreteTmA IABase.Get(TPK key)
{
}
ConcreteTmB IBBase.Get(TPK key)
{
}
}
这篇关于如何使用相同的接口与diferrent模板参数两次,在一个界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文