您的位置:首页 > 移动开发 > Unity3D

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: