Unity3D使用C#中的泛型和接口
2013-07-02 17:43
351 查看
http://blog.csdn.net/hiramtan/article/details/9071745
使用泛型最大的好处就是代码复用,比如写一段程序要统计"水"的一些属性(信息),如这是什么样的水(矿泉水/自来水/污水.etc),或者水的温度是多少.可以这样写(使用结构体):
[csharp] view
plaincopy
public class NewBehaviourScript : MonoBehaviour
{
// Use this for initialization
void Start()
{
Water test = new Water();
test.name = "KuangQuanShui";
test.temperature = 100;
Debug.Log("水的名字: " + test.name + " 水的温度: " + test.temperature);
}
}
public struct Water
{
public string name;//水的名字
public int temperature;//水的温度
}
下面是使用泛型来重写上面的这段代码,如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
Water<string> test1 = new Water<string>();
test1.info = "KuangQuanShui";//在此传入水的名字是"矿泉水"(可以将"T"看作是string类型)
Water<int> test2 = new Water<int>();
test2.info = 100;//在此传入水的温度是 100(可以将"T"看作是int类型)
Debug.Log("水的名字: " + test1.info + " 水的温度: " + test2.info);
}
}
public class Water<T>
{
public T info;//水的信息(属性)
}
上面的"T"可以看作是任意数据类型,(对于为何不使用基类型object及涉及的装箱/拆箱,网上介绍的很详细了,在这就不过多叙述了)通过上面的两段代码可能还看不出泛型有多大的好处,但是想想如果是使用泛型来初始化单例,是不是方便很多.
对于泛型的约束:如果我只允许"T"接收某一个数据类型(比如自定义了一个构造函数,只接收这个类型)如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
WaterBase waterInfo = new WaterBase();
Water<WaterBase> test3 = new Water<WaterBase>();//Water<限定只能为WaterBase类型> 可以将"T"看作是WaterBase类型
test3.info = waterInfo;
test3.info.name = "KuangQuanShui";
test3.info.temperature = 100;
Debug.Log("水的名字是: " + test3.info.name + " 水的温度是: " + test3.info.temperature);
}
}
public class Water<T> where T : WaterBase
{
public T info;//水的信息(属性) 可看作"T"的类型是WaterBase
}
public class WaterBase
{
public string name;
public int temperature;
}
泛型的约束,官网规定如下:
如果是多个占位符的泛型(两个),示例代码如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
//从A产线出来的生产日期是string类型的"20130610",水是矿泉水,温度是20,
Water<string, WaterBase> test1 = new Water<string, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于string类型
test1.data = "20130610";
test1.info.name = "KuangQuanShui";
test1.info.temperature = 20;
//从B产线出来的生产日期是int类型的20130610,水是纯净水,温度是20,
Water<int, WaterBase> test2 = new Water<int, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于int类型
test2.data = 20130610;
test2.info.name = "ChunJingShui";
test2.info.temperature = 20;
}
}
public class Water<T, U> where U : WaterBase //限定"U"只能接收WaterBase类型
{
public T data;//出厂日期(可接受int型的20130610,或者string类型的"20130610");
public U info;//水的具体信息(矿泉水/纯净水...温度)
}
public class WaterBase
{
public string name;
public int temperature;
}
接下来是泛型的继承,下面分别是一个占位符/多个占位符的继承示例,以及泛型继承的写法:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
TestChild1 test = new TestChild1();
test.data = "KuangQuanShui";
Testchild2 test2 = new Testchild2();
test2.data = 100;
Son1 son1 = new Son1();
son1.data1 = "KuangQuanShui";
son1.data2 = 100;
}
}
#region 一个占位符
public class Test<T>
{
public T data;
}
public class TestChild1 : Test<string> { }
//或者: public class TestChild1<T>: Test<string> { }
//或者: public class TestChild1<T>: Test<T> { }
public class Testchild2 : Test<int> { }
#endregion
#region 两个占位符
public class Fater<T, U>
{
public T data1;
public U data2;
}
public class Son1 : Fater<string, int> { }
//或者: public class Son1<T,U> : Fater<string, int> { }
//或者: public class Son1<T,U> : Fater<T, U> { }
#endregion
下面说一下泛型方法,感觉和泛型差不多(也是允许传入类型任意)就不过多叙述了,示例如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
Test test = new Test();
Debug.Log(test.Fanxing<string>("KuangQuanShui"));
Debug.Log(test.Fanxing<int>(100));
}
}
public class Test
{
public T Fanxing<T>(T t)
{
return t;
}
}
使用泛型最大的好处就是代码复用,比如写一段程序要统计"水"的一些属性(信息),如这是什么样的水(矿泉水/自来水/污水.etc),或者水的温度是多少.可以这样写(使用结构体):
[csharp] view
plaincopy
public class NewBehaviourScript : MonoBehaviour
{
// Use this for initialization
void Start()
{
Water test = new Water();
test.name = "KuangQuanShui";
test.temperature = 100;
Debug.Log("水的名字: " + test.name + " 水的温度: " + test.temperature);
}
}
public struct Water
{
public string name;//水的名字
public int temperature;//水的温度
}
下面是使用泛型来重写上面的这段代码,如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
Water<string> test1 = new Water<string>();
test1.info = "KuangQuanShui";//在此传入水的名字是"矿泉水"(可以将"T"看作是string类型)
Water<int> test2 = new Water<int>();
test2.info = 100;//在此传入水的温度是 100(可以将"T"看作是int类型)
Debug.Log("水的名字: " + test1.info + " 水的温度: " + test2.info);
}
}
public class Water<T>
{
public T info;//水的信息(属性)
}
上面的"T"可以看作是任意数据类型,(对于为何不使用基类型object及涉及的装箱/拆箱,网上介绍的很详细了,在这就不过多叙述了)通过上面的两段代码可能还看不出泛型有多大的好处,但是想想如果是使用泛型来初始化单例,是不是方便很多.
对于泛型的约束:如果我只允许"T"接收某一个数据类型(比如自定义了一个构造函数,只接收这个类型)如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
WaterBase waterInfo = new WaterBase();
Water<WaterBase> test3 = new Water<WaterBase>();//Water<限定只能为WaterBase类型> 可以将"T"看作是WaterBase类型
test3.info = waterInfo;
test3.info.name = "KuangQuanShui";
test3.info.temperature = 100;
Debug.Log("水的名字是: " + test3.info.name + " 水的温度是: " + test3.info.temperature);
}
}
public class Water<T> where T : WaterBase
{
public T info;//水的信息(属性) 可看作"T"的类型是WaterBase
}
public class WaterBase
{
public string name;
public int temperature;
}
泛型的约束,官网规定如下:
如果是多个占位符的泛型(两个),示例代码如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
//从A产线出来的生产日期是string类型的"20130610",水是矿泉水,温度是20,
Water<string, WaterBase> test1 = new Water<string, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于string类型
test1.data = "20130610";
test1.info.name = "KuangQuanShui";
test1.info.temperature = 20;
//从B产线出来的生产日期是int类型的20130610,水是纯净水,温度是20,
Water<int, WaterBase> test2 = new Water<int, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于int类型
test2.data = 20130610;
test2.info.name = "ChunJingShui";
test2.info.temperature = 20;
}
}
public class Water<T, U> where U : WaterBase //限定"U"只能接收WaterBase类型
{
public T data;//出厂日期(可接受int型的20130610,或者string类型的"20130610");
public U info;//水的具体信息(矿泉水/纯净水...温度)
}
public class WaterBase
{
public string name;
public int temperature;
}
接下来是泛型的继承,下面分别是一个占位符/多个占位符的继承示例,以及泛型继承的写法:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
TestChild1 test = new TestChild1();
test.data = "KuangQuanShui";
Testchild2 test2 = new Testchild2();
test2.data = 100;
Son1 son1 = new Son1();
son1.data1 = "KuangQuanShui";
son1.data2 = 100;
}
}
#region 一个占位符
public class Test<T>
{
public T data;
}
public class TestChild1 : Test<string> { }
//或者: public class TestChild1<T>: Test<string> { }
//或者: public class TestChild1<T>: Test<T> { }
public class Testchild2 : Test<int> { }
#endregion
#region 两个占位符
public class Fater<T, U>
{
public T data1;
public U data2;
}
public class Son1 : Fater<string, int> { }
//或者: public class Son1<T,U> : Fater<string, int> { }
//或者: public class Son1<T,U> : Fater<T, U> { }
#endregion
下面说一下泛型方法,感觉和泛型差不多(也是允许传入类型任意)就不过多叙述了,示例如下:
[csharp] view
plaincopy
public class fanxing : MonoBehaviour
{
// Use this for initialization
void Start()
{
Test test = new Test();
Debug.Log(test.Fanxing<string>("KuangQuanShui"));
Debug.Log(test.Fanxing<int>(100));
}
}
public class Test
{
public T Fanxing<T>(T t)
{
return t;
}
}
相关文章推荐
- C#中的枚举使用泛型接口和非泛型接口实现
- C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法
- C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法
- 编写高质量代码改善C#程序的157个建议——建议42:使用泛型参数兼容泛型接口的不可变性
- C# 接口的定义和使用
- 泛型接口的使用
- 【C#食谱】【面食】菜单1: 何时何地使用泛型
- c#使用接口--帮助你更好的理解接口(转载)
- c# 使用泛型序列化
- unity3d中对枚举的使用--高度提取接口方法
- Unity3d中C#使用指针(Unsafe)的办法 宏 预处理
- 在C#中使用MSHTML的高级支持接口
- 看书笔记——《Unity3d脚本编程使用C#语言开发跨平台游戏》——U3d游戏脚本中的值类型
- 转 C#开发微信门户及应用(1)--开始使用微信接口
- 何时使用委托而不使用接口(C# 编程指南)
- C#使用泛型的栈的示例
- C#:泛型集合—Dictionary<K,V>使用技巧
- 泛型接口(C# 编程)