如何解决如何混合泛型和实数类型
| 为什么不允许有这样的泛型类:class SomeClass<T,int> { ..... }
也可以将某些类型T强制为数字,并支持+,-,*和/等基本运算符。
解决方法
因为泛型与C ++模板不同;允许部分专业化。
当然,您可以拥有:
public class Generic<T,U> {}
public class Generic2<T> : Generic<T,int> {}
只要第一个泛型中出现在T
上的任何泛型约束至少对第二个泛型重复或更严格。
至于您要求的限制,不可以。但是,您可以限制为值类型(当然是where T : struct
);但这并不真正限制为“数字”。
由于运算符是静态定义的,因此对于运算符a op b
可以出现在两种类型T(a)
或ѭ6either中,因此运算符不包含在类合同中(无论是通过基数还是通过接口)。
结果,不可能基于它们定义类型约束。
但是,您可以定义自己的合同-像这样(此处只是天真和完全不完整的实现,仅用于演示,in a wall of code(tm)
):
(在最后检查通用名称,以查看它是否有效)
public abstract class MyNumeric
{
public abstract object ValueObject { get; }
public abstract MyNumeric Add(MyNumeric other);
public abstract MyNumeric Substract(MyNumeric other);
public abstract MyNumeric Multiply(MyNumeric other);
public abstract MyNumeric Divide(MyNumeric other);
public static MyNumeric operator +(MyNumeric a,MyNumeric b)
{
return a.Add(b);
}
public static MyNumeric operator -(MyNumeric a,MyNumeric b)
{
return a.Substract(b);
}
public static MyNumeric operator *(MyNumeric a,MyNumeric b)
{
return a.Multiply(b);
}
public static MyNumeric operator /(MyNumeric a,MyNumeric b)
{
return a.Divide(b);
}
//etc
}
public abstract class MyNumeric<T> : MyNumeric
where T : struct
{
public override object ValueObject { get{ return this.Value;}}
public new T Value { get; private set; }
public MyNumeric(T value) { Value = value; }
}
public class MyInt : MyNumeric<int>
{
public MyInt(int value) : base(value) { }
public override MyNumeric Add(MyNumeric other)
{
//could be really crafty here and use an interface instead that
//gives access to the Value part only - that way you could
//have MyDouble,for example,implement INumeric<int> explicitly
//via a c# explicit conversion.
MyNumeric<int> otherInt = other as MyNumeric<int>;
if (otherInt == null)
throw new ArgumentException(
\"Need to handle numeric promotion/demotion for all types\",\"other\");
return new MyInt(Value + otherInt.Value);
}
public override MyNumeric Divide(MyNumeric other)
{
throw new NotImplementedException();
}
public override MyNumeric Multiply(MyNumeric other)
{
throw new NotImplementedException();
}
public override MyNumeric Substract(MyNumeric other)
{
throw new NotImplementedException();
}
}
public class MyGeneric<TNumeric> where TNumeric : MyNumeric
{
public TNumeric WillNowCompile(TNumeric a,TNumeric b)
{
//cast is still required on the result.
//however - you can set the return type to MyNumeric instead if you want.
return (TNumeric)(a + b);
}
public TNumeric AnotherOne<TNumeric2>(TNumeric a,TNumeric2 b)
where TNumeric2 : MyNumeric
{
return (TNumeric)(a / b);
}
}
但是,正如我在代码中所说的那样,您将不得不考虑如何处理诸如int
+ѭ10situations之类的情况。但是可以以此为起点来完成。
,通用“ 2”参数允许与类使用的参数类型无关。
如果您想使用int
,请在课程中使用int
。如果您要规定类中的某些内容必须为“ 9”,则可以通过公共方法或构造函数的参数来实现。
对运算符的支持将取决于它自己传递的类型。如果您为该类型重写了运算符,则一旦在类中使用过它,它将支持该运算符。您不能对未知的通用名称执行此操作。
,对于问题的第二部分,您可以执行以下操作:
class MyClass<T> where T : IEquatable<T>,IComparable<T>
没有所有数字的父类,但是它们确实实现了这些接口。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。