首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

泛型入门 事例

2012-10-29 
泛型入门 例子总结:泛型的作用:类似功能的函数,但是类型不同,例如整型和字符串,则可使用泛型来进行整合,提

泛型入门 例子

总结:泛型的作用:类似功能的函数,但是类型不同,例如整型和字符串,则可使用泛型来进行整合,提高代码质量;?

?

?

//--定义一个泛型类

public class A<T>
??? {
??????? T str;
??????? public T GetVal()
??????? {

??????????? return str;
??????? }

??????? public void SetVal(T val)
??????? {

??????????? this.str = val;
??????? }?????????
??? }

?

//--进行调用

?

class test
??? {

?????????static void Main(string[] args)
??????? {
????????????A<string> ob = new A<string>();?? //--使用string型

??????????? ob.SetVal("hehe");
??????????? string str = ob.GetVal();

?????????? ?Console.WriteLine(str);

?


??????????? A<int> ob_int = new A<int>();
??????????? ob_int.SetVal(5);
??????????? int n = ob_int.GetVal();

?????????? Console.WriteLine(n);???????

???????????

??????????? Console.ReadKey();

??????? }

??? }

?

?

热点排行