蓝鸥Unity开发基础——泛型

2023-12-26 13:18

本文主要是介绍蓝鸥Unity开发基础——泛型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

蓝鸥Unity开发基础——泛型

本节课我们来学习C#中的泛型,泛型是一个特殊的类型,它可以最大限度的重用我们的代码!

使用泛型能够最大限度的重用代码,保护类型安全,提高性能

泛型成员因为类型的不确定性,不能使用算术运算符,比较运算符

类型参数可以有多个,可以是编译器能够识别的任何类型

类型参数的名字不能够随便起,不能重名


一、数组类Array

 

using System;

namespace Lesson_21
{
    //数组类Array
    public  class Array{

        public  int Count{
            get{
                return _count;
            }
            
        }
        public  void Add(int value){
            _arr[_count]=value;
            _count++;
        }

        public  void log(){
            //当前数组中包含3个元素:(13,76,92
            string str = "当前数组中包含"+Count+"个元素:(";
            for (int i = 0; i < Count; i++) {
                str+=_arr[i];
                if (i<Count-1) {
                    str +=", ";
                }

            }
            str += ")";

        }

        public Array(){
            _arr=new int[100];

        }

        private int[] _arr;
        private int _count=0;
        
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            Array arr = new Array ();
            arr.log ();
            arr.Add(13);
            arr.log ();
            arr.Add(76);            
            arr.log ();
            arr.Add(92);
            arr.log ();


        }
    }
}

二、索引器

using System;

namespace Lesson_21
{
    //数组类Array
    public  class Array{
        
        //索引器
        public int  this [int index] {
            set{
                _arr [index] = value;
                
            }
            get{
                return _arr[index];
            }


        }

        public  int Count{
            get{
                return _count;
            }
            
        }
        public  void Add(int value){
            _arr[_count]=value;
            _count++;
        }

        public  void log(){
            //当前数组中包含3个元素:(13,76,92
            string str = "当前数组中包含"+Count+"个元素:(";
            for (int i = 0; i < Count; i++) {
                str+=_arr[i];
                if (i<Count-1) {
                    str +=", ";
                }

            }
            str += ")";
            Console.WriteLine (str);

        }

        public Array(){
            _arr=new int[100];

        }

        private int[] _arr;
        private int _count=0;
        
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            Array arr = new Array ();
            arr.log ();
            arr.Add(13);
            arr.log ();
            arr.Add(76);            
            arr.log ();
            arr.Add(92);
            arr.log ();

            arr [0] = 26;
            arr.log ();
            Console.WriteLine ("arr[1]="+arr[1]);

        }
    }
}

三、数组中只能存整数,无法存小数的。我们在初始化和参数的时候,都确定了int类型。但是在使用的过程中,我们肯定有存其他类型的,那么我们怎么办?一种方式就是我们复制代码,修改类型,但是这种方式很麻烦。是否有更简便的方式。

大家不要忘记我们这节课的主题——泛型,那么我们就看一下泛型是如何使用的?

using System;

namespace Lesson_21
{
    //使用泛型
    //数组类Array
    //泛型类——需要在类名后加上泛型类型
    //定义的时候需要使用T泛型类型表示任意一种数据类型
    public  class Array<T>{
        
        //索引器
        public T  this [int index] {
            set{
                _arr [index] = value;
                
            }
            get{
                return _arr[index];
            }


        }

        public  int Count{
            get{
                return _count;
            }
            
        }
        public  void Add(T value){
            _arr[_count]=value;
            _count++;
        }

        public  void log(){
            //当前数组中包含3个元素:(13,76,92
            string str = "当前数组中包含"+Count+"个元素:(";
            for (int i = 0; i < Count; i++) {
                str+=_arr[i];
                if (i<Count-1) {
                    str +=", ";
                }

            }
            str += ")";
            Console.WriteLine (str);

        }

        public Array(){
            _arr=new T[100];

        }

        private T[] _arr;
        private int _count=0;
        
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            //泛型当具体使用的时候,才需要实际的类型
            Array<int> arr=new Array<int>();
            arr.log ();
            arr.Add (23);
            arr.log ();
            arr.Add (74);
            arr.log ();
            arr[1] =12;
            arr.log ();


        }
    }
}

 

修改一:存浮点类型

class MainClass
    {
        public static void Main (string[] args)
        {
            //泛型当具体使用的时候,才需要实际的类型
            Array<float> arr=new Array<float>();
            arr.log ();
            arr.Add (23.5f);
            arr.log ();
            arr.Add (74.12f);
            arr.log ();
            arr[1] =12.99f;
            arr.log ();
        } 

修改二:字符串类型

class MainClass
    {
        public static void Main (string[] args)
        {
            //泛型当具体使用的时候,才需要实际的类型
            Array<string> arr=new Array<string>();
            arr.log ();
            arr.Add ("老王");
            arr.log ();
            arr.Add("老张");
            arr.log ();
            arr[1] ="赵四";
            arr.log ();
        } 

课堂源代码:

using System;

namespace Lesson_21
{
    //使用泛型
    //数组类Array
    //泛型类——需要在类名后加上泛型类型
    //定义的时候需要使用T泛型类型表示任意一种数据类型
    //1、使用泛型能够提高代码重用
    //2、使用泛型是,由于类型不确定,所以我们不能使用算术运算符


//    public  class Math<T>{
//        public  T Max(T num1,T num2){
//            return num1 > num2 ? num1 : num2;

            
//        }
        
//    }

    //T ——type
    //S/U/V——234种类型
    //K/V——key/value
    //N——number
    public  class Array<T,S,U,V>{
        
        //索引器
        public T  this [int index] {
            set{
                _arr [index] = value;
                
            }
            get{
                return _arr[index];
            }


        }

        public  int Count{
            get{
                return _count;
            }
            
        }
        public  void Add(T value){
            _arr[_count]=value;
            _count++;
        }

        public  void log(){
            //当前数组中包含3个元素:(13,76,92
            string str = "当前数组中包含"+Count+"个元素:(";
            for (int i = 0; i < Count; i++) {
                str+=_arr[i];
                if (i<Count-1) {
                    str +=", ";
                }

            }
            str += ")";
            Console.WriteLine (str);

        }

        public Array(){
            _arr=new T[100];

        }

        private T[] _arr;
        private int _count=0;
        
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            //泛型当具体使用的时候,才需要实际的类型
            Array<string> arr=new Array<string>();
            arr.log ();
            arr.Add ("老王");
            arr.log ();
            arr.Add("老张");
            arr.log ();
            arr[1] ="赵四";
            arr.log ();

    
        }
    }
}

 

 

 

这篇关于蓝鸥Unity开发基础——泛型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/539549

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部