首页 / C# / c# – 如何混合通用和真实类型
c# – 如何混合通用和真实类型
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何混合通用和真实类型,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2818字,纯文字阅读大概需要5分钟。
内容图文
为什么不允许这样的泛型类:
class SomeClass<T, int> { ..... }
也可以强制某些类型T为数字并支持基本运算符,如, – ,*和/.
解决方法:
因为泛型与C模板不同;允许部分专业化.
当然你可以这样:
public class Generic<T, U> {}
public class Generic2<T> : Generic<T, int> {}
只要第一个通用中的T上存在的任何通用约束至少重复,或者对第二个通则约束更严格.
至于你要求的限制,不,你不能.但是,您可以限制值类型(当然T:struct);但这并不是真的限制为“数字”.
由于运算符是静态定义的,并且对于表达式op b,运算符不包含在类的合同(基本或接口)的一部分中,因此可以出现在T(a)或T这两种类型中的任何一种上. (b)中.
因此,无法根据它们定义类型约束.
但是,您可以定义自己的合同 – 像这样(这里的天真和完全不完整的实现,只是为了演示代码墙(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 double这样的情况.但是可以用它作为起点来完成.
内容总结
以上是互联网集市为您收集整理的c# – 如何混合通用和真实类型全部内容,希望文章能够帮你解决c# – 如何混合通用和真实类型所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。