尝试为同一个类实现两次接口时遇到编译器错误,如下所示:
public class Mapper<T1, T2> : IMapper<T1, T2>, IMapper<T2, T1>
{
/* implementation for IMapper<T1, T2> here. */
/* implementation for IMapper<T2, T1> here. */
}
Run Code Online (Sandbox Code Playgroud)
错误:
'Mapper'无法同时实现'IMapper'和'IMapper',因为它们可能会统一某些类型参数替换.
为什么这个解决方法有效?我想知道我是否已经解决了问题或者只是欺骗了编译器.
public class Mapper<T1, T2> : MapperBase<T1, T2>, IMapper<T1, T2>
{
/* implementation for IMapper<T1, T2> here. */
}
public class MapperBase<T1, T2> : IMapper<T2, T1>
{
/* implementation for IMapper<T2, T1> here. */
}
Run Code Online (Sandbox Code Playgroud)
编辑:我已经更新MyClass,MyClassBase以及IMyInterface向Mapper,MapperBase以及IMapper代表一个更真实的场景,这个问题可以提出自己.
我正在尝试在一系列项目上运行 async.each。
对于每个项目,我想运行一个 async.waterfall。请参阅下面的代码。
var ids = [1, 2];
async.each(ids,
function (id, callback) {
console.log('running waterfall...');
async.waterfall([
function (next) {
console.log('running waterfall function 1...');
next();
},
function (next) {
console.log('running waterfall function 2...');
next();
}],
function (err) {
if (err) {
console.error(err);
}
else {
console.log('waterfall completed successfully!');
}
callback();
});
},
function (err) {
if (err) {
console.error(err);
}
else {
console.log('each completed successfully!');
}
});
return;
Run Code Online (Sandbox Code Playgroud)
此代码的输出如下所示:
running waterfall...
running waterfall function 1...
running waterfall...
running waterfall function …Run Code Online (Sandbox Code Playgroud)