一.lambda表达式
1.简单示例(冒泡排序法,比较与数组中最大的数进行比较大小)
public delegate bool Dele<T>(T num1,T num2);
static void Main(string[] args)
{
Getmax(
(a, b) => {
int sum = a + b;
if (sum > 0)
{
return true;
}
if (sum<0)
{
return false;
}
return false; }
,1,2,3,4,5,6);
}
public static void Getmax<T>(Dele<T> del,params T[] nums)
{
T max = nums[0];
for (int i = 0; i < nums.Length-1; i++)
{
for (int j = 1; j < nums.Length-1-i; j++)
{
//if 第一个数小于第二个数 就往后移动
if (del(nums[i],nums[j]))
{
T temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
}
二.泛型委托
1.Func<T,T...,返回值类型> 带有返回值类型的委托 最后一个参数永远是返回值
Func<int> f = () =>{ return 1; };
2.Action<> 不带有返回值类型的委托
Action<int> s = (a) => { Console.WriteLine(a); };
三.多播委托
1.一个委托变量可以绑定多个方法,当没有方法时,则不再可以使用 “-=”
public delegate void Dele();
static void Main(string[] args)
{
Dele d=Eating;
d += Drink;
d();
d -= Drink;
d();
Console.ReadKey();
}
static void Eating()
{
Console.WriteLine("吃饭");
}
static void Drink()
{
Console.WriteLine("喝水");
}
四.复习
1.静态类和非静态类
(1)静态类:
静态类内部成员的调用 类名.方法名 来直接调用
静态类占用内存,所以理论上在整个项目中越少越好
静态类中的方法都是静态的
工具类: 如Console
(2)应用示例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ArrayHelp;
public class ArrayHelperTest : MonoBehaviour {
void OnGUI()
{
if (GUILayout.Button("血量大于50的敌人"))
{
Enemy[] allEnemy = FindObjectsOfType<Enemy>();
Enemy[] enemy = ArrayHelper.FindAll(allEnemy, (e) => e.Hp > 50);
foreach (var item in enemy)
{
Debug.Log(item);
}
}
if (GUILayout.Button("血量最高的敌人"))
{
Enemy[] allEnemy = FindObjectsOfType<Enemy>();
Enemy enemy = ArrayHelper.GatMax(allEnemy, (e) => e.Hp);
print(enemy);
}
if (GUILayout.Button("血量最低的敌人"))
{
Enemy[] allEnemy = FindObjectsOfType<Enemy>();
Enemy enemy = ArrayHelper.GatMin(allEnemy, (e) => e.Hp);
print(enemy);
}
if (GUILayout.Button("距离最近的敌人"))
{
Enemy[] allEnemy = FindObjectsOfType<Enemy>();
Enemy enemy = ArrayHelper.GatMin(allEnemy, (e) =>Vector3.Distance(transform.position,e.transform.position));
print(enemy);
}
if (GUILayout.Button("检测带有Enemy标签的敌人"))
{
GameObject[] AllEnemy= GameObject.FindGameObjectsWithTag("Enemy");
Enemy[] enemy= ArrayHelper.Selet(AllEnemy, e => e.GetComponent<Enemy>());
foreach (var item in enemy)
{
print(item);
}
}
}
}
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ArrayHelp
{
class ArrayHelper
{
/// <summary>
/// 查找满足所有条件的元素
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="array">数组</param>
/// <param name="condition">传递过来的自定义判断方法</param>
/// <returns></returns>
public static T Find<T>(T[] array, Func<T, bool> condition)
{
for (int i = 0; i < array.Length; i++)
{
if (condition(array[i]))
{
return array[i];
}
}
//泛型的默认值
return default(T);
}
/// <summary>
/// 返回满足条件的所有内容
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static T[] FindAll<T>(T[] array, Func<T, bool> condition)
{
List<T> list = new List<T>();
for (int i = 0; i < array.Length; i++)
{
if (condition(array[i]))
{
list.Add(array[i]);
}
}
//将list转换为数组
return list.ToArray();
}
/// <summary>
/// 降序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Q"></typeparam>
/// <param name="array"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static T[] GatDown<T,Q>(T[] array,Func<T,Q> condition) where Q:IComparable
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (condition(array[i]).CompareTo(condition(array[j]))<0)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
/// <summary>
/// 升序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Q"></typeparam>
/// <param name="array"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static T[] GatUP<T, Q>(T[] array, Func<T, Q> condition) where Q : IComparable
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (condition(array[i]).CompareTo(condition(array[j])) > 0)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
/// <summary>
/// 获取最小值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Q"></typeparam>
/// <param name="array"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static T GatMin<T, Q>(T[] array, Func<T, Q> condition) where Q : IComparable
{
T min = array[0];
for (int i = 0; i < array.Length - 1; i++)
{
if (condition(min).CompareTo(condition(array[i])) > 0)
{
min = array[i];
}
}
return min;
}
/// <summary>
/// 获取最大值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Q"></typeparam>
/// <param name="array"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static T GatMax<T, Q>(T[] array, Func<T, Q> condition) where Q : IComparable
{
T max = array[0];
for (int i = 0; i < array.Length - 1; i++)
{
if (condition(max).CompareTo(condition(array[i])) < 0)
{
max = array[i];
}
}
return max;
}
public static Q[] Selet<T,Q>(T[] array, Func<T, Q> condition)
{
Q[] result = new Q[array.Length];
for (int i = 0; i < array.Length; i++)
{
result[i] = condition(array[i]);
}
return result;
}
}
}
3.静态构造函数例题
namespace StaticTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(A.a + " "+B.b);
Console.ReadKey();
}
}
class A
{
public static int a;
static A()
{
a = B.b + 1;
}
}
class B
{
public static int b;
static B()
{
b = A.a + 1;
}
}
}
静态构造函数是当类中的成员被使用时,静态构造函数将会被系统自动调用,并且只会调用一次
结果为 a=2 b=1