我正在研究惰性对象作曲家
class Bitset
{
private List<bool> _data;
public Bitset(List<bool> vector)
{
_data = vector;
}
public virtual bool GetElement (int i)
{
return _data[i];
}
public Bitset(){}
}
class BitsetComposer:Bitset
{
readonly private Bitset _a,_b;
private Func<bool,bool,bool> _composer;
public BitsetComposer(Bitset a,Bitset b, Func<bool,bool,bool> composer)
{
this._a=a;
this._b=b;
this._composer=composer;
}
public override bool GetElement (int i)
{
return _composer(_a.GetElement(i),_b.GetElement(i));
}
public static BitsetComposer operator & (BitsetComposer a, BitsetComposer b)
{
return new BitsetComposer(a,b,BitsetComposer.And);
}
public static bool And(bool a,bool b){return a&b;}
}
这不好,因为希望 & 也可以采用基类
//replaced with
//public static BitsetComposer operator & (BitsetComposer a, BitsetComposer b)
public static BitsetComposer operator & (Bitset a, Bitset b)
{
return new BitsetComposer(a,b,BitsetComposer.And);
}
但我得到了错误:
错误 CS0563:二元运算符的参数之一必须是包含类型