关于泛型的一个小问题!

hk8846 2009-04-09 10:52:25

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 泛型接口
{
public interface Add<T>
{
T add(T x, T y);
}

class math<T> : Add<T>
{
public T add(T x, T y)
{
return x + y;
}
}

class Program
{
static void Main(string[] args)
{
math<int> s = new math<int>();
int z = s.add(4, 7);
Console.WirteLine(z);
Console.Read();
}
}
}
...全文
204 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztenv 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 whlinhai 的回复:]
引用 9 楼 lianshaohua 的回复:
因为在这里T可能为任意的类型,当你自己定义了一个类而又没有重载+操作符时,当然不能成功;
解决:加入类型约束,把T约束为值类型(public interface Add <T> T where T:struct);或实现了+操作符的派生类型;

建议:看看接口约束/派生约束/构造函数约束/引用类型和值类型约束


1、struct并没有实现+操作符,所以添加此约束并不能解决问题;
2、如果你自定义一个含+操作符的接口,那…
[/Quote]

泛型也不是万能的,值类型的约整和引用类型的约束还是有很大区别的,重载+操作符吧;
Ricercar 2009-04-10
  • 打赏
  • 举报
回复
重载+操作符
whlinhai 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lianshaohua 的回复:]
因为在这里T可能为任意的类型,当你自己定义了一个类而又没有重载+操作符时,当然不能成功;
解决:加入类型约束,把T约束为值类型(public interface Add <T> T where T:struct);或实现了+操作符的派生类型;

建议:看看接口约束/派生约束/构造函数约束/引用类型和值类型约束
[/Quote]

1、struct并没有实现+操作符,所以添加此约束并不能解决问题;
2、如果你自定义一个含+操作符的接口,那就不能用int,float等值类型去实例化,因为它们并没有继承你的接口。
ztenv 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 typeof 的回复:]
C# codeusing System;

namespace 泛型接口
{
public interface Add<T>
{
string add(T x, T y);
}

class math<T> : Add<T>
where T: IConvertible
{
public string add(T x, T y)
{
return (x.ToInt32(null) + y.ToInt32(null)).ToString();
}
}

class Program
{
static void Main(string[] args)
{

[/Quote]


如果我传入的T是个浮点数怎么办?如果传入的是一个实现了+操作符的类,你又当如何处理呢?

如果像上面这种写法,还不如不用泛型,直接写一个整型变量相加的方法就算了;
typeof 2009-04-09
  • 打赏
  • 举报
回复
using System;

namespace 泛型接口
{
public interface Add<T>
{
string add(T x, T y);
}

class math<T> : Add<T>
where T: IConvertible
{
public string add(T x, T y)
{
return (x.ToInt32(null) + y.ToInt32(null)).ToString();
}
}

class Program
{
static void Main(string[] args)
{
math<int> s = new math<int>();
int z = int.Parse(s.add(4, 7));
Console.WriteLine(z);
Console.Read();
}
}
}
ztenv 2009-04-09
  • 打赏
  • 举报
回复
因为在这里T可能为任意的类型,当你自己定义了一个类而又没有重载+操作符时,当然不能成功;
解决:加入类型约束,把T约束为值类型(public interface Add<T> T where T:struct);或实现了+操作符的派生类型;

建议:看看接口约束/派生约束/构造函数约束/引用类型和值类型约束
typeof 2009-04-09
  • 打赏
  • 举报
回复
using System;

namespace 泛型接口
{
public interface Add<T>
{
int add(T x, T y);
}

class math<T> : Add<T>
where T: IConvertible
{
public int add(T x, T y)
{
return x.ToInt32(null) + y.ToInt32(null);
}
}

class Program
{
static void Main(string[] args)
{
math<int> s = new math<int>();
int z = s.add(4, 7);
Console.WriteLine(z);
Console.Read();
}
}
}
lzf1988119 2009-04-09
  • 打赏
  • 举报
回复
如果你就是想做加法的那不如直接把t换成int好了
ojekleen 2009-04-09
  • 打赏
  • 举报
回复
T 不一定就是你所用的int ,如果是其他类型怎么办?所以整不成的。。
  • 打赏
  • 举报
回复
是啊T是未知的类型,你把T换成int
路人乙e 2009-04-09
  • 打赏
  • 举报
回复
Convert.ToInt32(x)+Convert.ToInt32(y)
cja03 2009-04-09
  • 打赏
  • 举报
回复
类型是未知的,两个类型“相加”,倒底要进行怎么样的运算,也是不可预料的。
所以,不能确定运算符要如何去重载。
wts_net 2009-04-09
  • 打赏
  • 举报
回复
不是这样子用的
hk8846 2009-04-09
  • 打赏
  • 举报
回复
问题是:编译该程序的时候会编译失败,错误为-Error Info: 运算符“+”无法应用于“T”和“T”类型的操作数,怎么修改才能让该段代码正常的运行起来呢,请各位帮忙看先,感谢先。。。。。!!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧