泛型学习
using System;
namespace ConsoleApp
{
class FormalParameters
{
public static void Main()
{
/* 旧方法
double x = 5;
double y = 10;
CzMath c = new CzMath();
Console.WriteLine ("Before x={0},y={1}",x,y);
c.Swap (ref x,ref y);
Console.WriteLine ("After x={0},y={1}",x,y);
*/
//使用泛型类
int i1 = 5, i2 = 10;
Console.WriteLine("Before i1={0},i2={1}", i1, i2);
CzMath<int>.Swap(ref i1,ref i2);
Console.WriteLine("After i1={0},i2={1}", i1, i2);
double d1 = 5, d2 = 10;
Console.WriteLine("Before d1={0},d2={1}", d1, d2);
CzMath<double>.Swap(ref d1, ref d2);
Console.WriteLine("After d1={0},d2={1}", d1, d2);
Console.Read();
}
}
/* 旧方法
class CzMath
{
public void Swap(ref double x, ref double y)
{
double temp = x;
x = y;
y = temp;
}
public void Swap(ref int x, ref int y)
{
double temp = x;
x = y;
y = temp;
}
}
*/
//泛型类
class CzMath<T>
{
public static void Swap(ref T x, ref T y)
{
T tmp = x;
x = y;
y = tmp;
}
}
}
1.泛型集合
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp2
{
class GenericAssemble
{
public static void Main()
{
CzAssemble<Contact> asm1 = new CzAssemble<Contact>(3);
asm1[0] = new Contact("赵丽");
asm1[1] = new Contact("Tom");
asm1[2] = new Contact("李明");
CzAssemble<int> asm2 = new CzAssemble<int>(6);
for (int i = 0; i < 6; i++)
{
asm2[i] = i * 2 + 1;
}
asm1.Output();
asm2.Output();
Console.Read();
}
}
//泛型类: 集合CzAssemble<T>
public class CzAssemble<T>
{
protected T[] m_list;
public int length
{
get { return m_list.Length; }
}
public T this[int index]
{
get{return m_list [index];}
set{m_list [index]=value;}
}
public CzAssemble (int length)
{
m_list =new T[length];
}
public void Output()
{
foreach (T t in m_list )
Console .Write("{0}, ",t);
Console.WriteLine();
}
public static bool operator == (CzAssemble<T> asm1,CzAssemble<T> asm2 )
{
if(asm1.length != asm2.length )
return false;
for (int i=0;i<asm1.length ;i++)
if (asm1[i].Equals(asm2[i]))
return false;
return true ;
}
public static bool operator != (CzAssemble <T> asm1,CzAssemble<T> asm2)
{
return !(asm1==asm2);
}
}
public class Contact
{
protected string m_name;
public Contact (string name)
{
m_name = name;
}
public override string ToString()
{
return m_name;
}
}
}
赵丽, Tom, 李明,
1, 3, 5, 7, 9, 11,
----------------------------
2.成员与类型参数
嵌套泛型类:
泛型类OuterClass<T>不能使用内部类型参数S来定义字段, 而InnerClass<S>则可以使用外部类型参数T
class OuterClass<T>
{
private T m_v1;
//private S m_v2; //错误:不能使用内部类型参数
public void OutMethod()
{
InnerClass<int> inner = new InnerClass<int>(m_v1);
inner.InnerMethod();
}
public class InnerClass<S> //嵌套泛型成员
{
private T m_v1;
private S m_v2;
public InnerClass(T t1)
{
m_v1 = t1;
}
public void InnerMethod()
{
OuterClass<double> outer = new OuterClass<double>();
outer.OutMethod();
}
}
}
4.泛型的静态成员
泛型类的静态成员 既不属于泛型类的某个实例, 也不属于泛型类, 而是属于泛型类的构造类型.
对于普通类, 一个静态字段在内存中只有一份备份
对于泛型类,一个静态字段就在内存中拥有多份备份.
对于 泛型类的 静态构造函数, 按 每个构造类型 仅 调用一次!
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp2
{
class GenericStatic
{
public static void Main()
{
Contact<int> c1 = new Contact<int>("赵丽");
Contact<int> c2 = new Contact<int>("Tom");
Contact<double> c3 = new Contact<double>("李明");
Console.Read();
}
}
public class Contact<T>
{
private string m_name;
private static int m_objects = 0;
private static int m_classes = 0;
public Contact(string name)
{
Console.WriteLine("构造对象: "+name);
m_name = name;
m_objects++;
Console.WriteLine("Contact<{0}>: {1}", typeof(T), m_objects);
}
static Contact()
{
Console.WriteLine("构造类Contact<{0}>",typeof(T));
m_classes++;
Console.WriteLine("Contact<{0}>类数量: {1}",typeof(T),m_classes);
}
}
}
构造类Contact<System.Int32>
Contact<System.Int32>类数量: 1
构造对象: 赵丽
Contact<System.Int32>: 1
构造对象: Tom
Contact<System.Int32>: 2
构造类Contact<System.Double>
Contact<System.Double>类数量: 1
构造对象: 李明
Contact<System.Double>: 1
浙公网安备 33010602011771号