Jtro的技术分享:Unity中使用泛型

作者: UnityPlane | 来源:发表于2017-09-13 11:03 被阅读315次

泛型与结构体很像,都是复用一段代码。但是随着编程技巧的提升,结构体已经不能满足“优雅”的编程的需要了。所以泛型是最好的选择。

新建一个unity空工程,在里面新建一个空物体,在空物体上挂个脚本:Water

输入下面的代码:

using System.Collections;

using System.Collections.Generic;using

 UnityEngine;public class Water : MonoBehaviour {

// Use this for initialization

void Start () 

{

Fwatertest = new Fwater();//实例出一个泛型

test .info = "瓶装水";

Fwatertest1 = new Fwater();//实例出一个泛型

test1 .info = 5;

Debug.Log ("水的种类: "+test .info +" , "+"水的价格: "+test1 .info+"元");}//End of Start

 }

public class Fwater//泛型类

{

public T info;//水的信息,可以是任何种类

}

运行的结果

上面的就是最简单的泛型

下面的泛型可以是传多个,还可以限定泛型的类别:

新建一个工程:WaterPlus。新建一个空物体,新建一个WaterPlus脚本编写如下脚本

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class WaterPlus : MonoBehaviour

{

// Use this for initialization

void Start ()

 {

WaterplusClass < string , WaterBase> test = new WaterplusClass< string, WaterBase>();//实例一个test泛型

test.date = "2017.9.11";

test.info = new WaterBase ();test.info.zhonlei = "瓶装水";test.info.wendu = 20;

Debug.Log ("水的生产日期: "+test .date +" ,"+"水的种类: " +test .info .zhonlei+"水的温度: "+ test .info .wendu);

WaterplusClasstest1 = new WaterplusClass();//实例一个test1 泛型

test1.date = 20170911;

test1.info = new WaterBase ();

test1.info.zhonlei = "桶装水";

test1.info.wendu = 15;

Debug.Log ("水的生产日期: "+test1 .date +" ,"+"水的种类: " +test1 .info .zhonlei+"水的温度: "+ test1 .info .wendu);

}//End of Start

}

再新建一个脚本,不继承mono,编写如下脚本:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;//不继承mono

public class WaterplusClasswhere U : WaterBase //限定U只能接收waterbase类型的数据

{

public T date;//水的生产日期

public U info;//水的种类和价格

}

public class  WaterBase

{

public string zhonlei;

public int wendu;

}

运行之后:

运行结果

最后是泛型方法,同样是新建scene,新建空物体,新建脚本fxfangfa,编写如下脚本:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;public class FXFangFa : MonoBehaviour {

// Use this for initialization

void Start () {

Test test = new Test ();

string a =  test.Fx("FangXing");

int s =  test.Fx(12);Debug.Log (a + s);

}//End of Start 

// Update is called once per frame

void Update () {}//End of Update

}public class Test //类中的泛型方法{public T Fx(T t)

{

return t;

}

}

运行结果

好了泛型的使用就介绍这么多,感谢简书,感谢你的阅读。

相关文章

  • Jtro的技术分享:Unity中使用泛型

    泛型与结构体很像,都是复用一段代码。但是随着编程技巧的提升,结构体已经不能满足“优雅”的编程的需要了。所以泛型是最...

  • Java-API-集合框架(三)-泛型

    泛型的由来和基本使用 泛型的擦除 泛型类的使用 泛型方法的使用 泛型接口 泛型通配符(?) 通配符? 在api中的...

  • Kotlin 泛型

    Kotlin 泛型 1. 泛型类 定义一个泛型类 使用 在继承中 使用 2. 泛型函数 使用 3. 泛型的擦除 无...

  • Jtro的技术分享:unity的串口通讯

    今天一天时间都在面向Google编程.这也蛮头疼的,哈哈 串行接口(串口)通常指COM接口,是采用串行通信方式的扩...

  • 15、泛型常用特点

    泛型常用特点 泛型是Java SE 1.5之后的特性,《Java 核心技术》中对泛型的定义是: “泛型” 意味着编...

  • 四、Java高级--1、泛型

    泛型定义:数据类型参数化,提前定义好集合中放入什么类型集合框架中没使用泛型和使用泛型的比较 泛型规则和限制1、泛型...

  • 夯实JAVA基础之 - 泛型

    泛型的定义及使用 1. 定义泛型: 2. 类中使用泛型 3. 使用泛型类 4. 使用泛型的优势? 多泛型变量的定义...

  • Java高级特性入门——泛型、反射和注解

    本次的分享主要围绕以下三个方面: 一、泛型介绍 二、反射机制 三、注解的使用 一、泛型介绍 在日常编程的过程中,泛...

  • Jtro的技术分享:Unity中动态的换材质球

    其实换材质球是不现实的,核心就是换材质球的贴图。具体的代码如下: 很简单,但是用到的时候有人会不知道怎么办。

  • Jtro的技术分享:Unity中读取PC的硬件信息

    这个功能似乎没有什么用,但是我在《c#入门到精通》这本书中看到这样的功能,在非unity开发的项目中,假如说你是一...

网友评论

    本文标题:Jtro的技术分享:Unity中使用泛型

    本文链接:https://www.haomeiwen.com/subject/sisksxtx.html