会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
.NET 博客
.NET学习
博客园
首页
新随笔
联系
订阅
管理
★下边的这个应用是泛型的又一种用法
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication5
{
class
Program
{
static
void
Main(
string
[] args)
{
test
<
testType
>
ti
=
new
test
<
testType
>
(
4
);
ti.foo();
Console.WriteLine(ti.DataInt);
}
}
class
test
<
T
>
where T : ITest,
new
()
//
new()的作用是能够把传递进的的参数类型实例子化
//
在这个限制语句规定了在传递进来的参数类型要实现接口ITest
{
public
test(
int
i)
{
dataT
=
new
T();
//
等同于 System.Activator.CreateInstance<T>();仍然是用反射机制来获取泛型对象的实例的。
dataInt
=
i;
}
public
void
foo()
{
dataT.testMethod();
}
public
T DataT
{
get
{
return
dataT; }
}
public
int
DataInt
{
get
{
return
dataInt; }
}
private
T dataT;
private
int
dataInt;
}
interface
ITest
{
void
testMethod();
}
class
testType : ITest
{
public
void
testMethod()
{
Console.WriteLine(
"
testing
"
);
}
}
}
Posted on
2006-09-20 08:50
李通通
阅读(
203
) 评论(
0
)
收藏
举报
刷新页面
返回顶部