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  李通通  阅读(196)  评论(0编辑  收藏  举报