public class Racional<T>
{
private T nominator;
private T denominator;
public T Nominator
{
get { return nominator; }
set { nominator = value; }
}
public T Denominator
{
get { return denominator; }
set { denominator = value; }
}
public Racional(T nominator, T denominator)
{
this.nominator = nominator;
this.denominator = denominator;
}
public static Racional<int> operator *(Racional<int> a, Racional<int> b)
{
return ((int)(a.nominator + b.nominator, a.denominator + b.denominator));
}
public override string ToString()
{
return "(" + this.nominator + " " + this.denominator + ")";
}
}
我感興趣的是這一部分:幫助在課堂上的數學操作數(C#)
public static Racional<int> operator *(Racional<int> a, Racional<int> b)
{
return ((int)(a.nominator + b.nominator, a.denominator + b.denominator));
}
有什麼不對:
一個二元運算符的參數必須是包含類型
如何正常地編寫數學運算的這部分?
BTW,這是一門功課,或者你打算在生產代碼中使用它?檢出http://msdn.microsoft.com/en-us/library/microsoft.solverfoundation.common.rational%28v=vs.93%29.aspx – 2011-04-14 12:04:56
您可能希望將類名更改爲Rational ... – 2011-04-14 12:05:30
爲什麼你要創建一個由框架處理的類?如果這是家庭作業,這是一個可怕的做法。 – 2011-04-14 12:06:42