| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 769 人关注过本帖
标题:请教,关于泛型工厂~
只看楼主 加入收藏
zuiaibiancheng
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-28
收藏
 问题点数:0 回复次数:2 
请教,关于泛型工厂~
请问 ITraffic where T : new() 这段代码是什么意思?


using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace 工厂模式
{
    interface ITraffic
    {
        void CreateTool();
    }

    class Factory<T> : ITraffic where T : new()
    {
        public Factory()
        {
        }

        public void CreateTool()
        {
            T tool = new T();
        }
    }

    class Car
    {
        public Car()
        {
            Console.WriteLine("汽车Car");
        }
    }

    class Moto
    {
        public Moto()
        {
            Console.WriteLine("摩托Moto");
        }
    }

    class TestMain
    {
        static void Main(string[] args)
        {
            ITraffic car = new Factory<Car>();
            car.CreateTool();

            ITraffic moto = new Factory<Moto>();
            moto.CreateTool();

            Console.Read();
        }
    }
}
搜索更多相关主题的帖子: 泛型 
2008-07-12 19:54
zuiaibiancheng
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-28
收藏
得分:0 
问题已解决, where T : new() 为构造函数约束
2008-07-12 22:56
fjfhnui
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2007-6-18
收藏
得分:0 
对,是泛型约束
2008-07-14 16:29
快速回复:请教,关于泛型工厂~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024026 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved